001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.chart; 020 021import java.awt.Font; 022import java.awt.FontMetrics; 023import java.text.AttributedString; 024 025import lucee.commons.lang.StringList; 026import lucee.commons.lang.font.FontUtil; 027import lucee.runtime.op.Caster; 028import lucee.runtime.type.util.ListUtil; 029 030import org.jfree.chart.labels.PieSectionLabelGenerator; 031import org.jfree.data.general.PieDataset; 032 033public class PieSectionLegendLabelGeneratorImpl implements 034 PieSectionLabelGenerator { 035 036 037 private FontMetrics metrics; 038 private int with; 039 040 public PieSectionLegendLabelGeneratorImpl(Font font,int with) { 041 this.metrics=FontUtil.getFontMetrics(font); 042 this.with=with-20; 043 } 044 045 public AttributedString generateAttributedSectionLabel(PieDataset dataset, 046 Comparable key) { 047 return null; 048 } 049 050 public String generateSectionLabel(PieDataset pd, Comparable c) { 051 String value=Caster.toString(pd.getKey(pd.getIndex(c)),""); 052 053 StringList list = ListUtil.toList(value, '\n'); 054 StringBuilder sb=new StringBuilder(); 055 String line; 056 int lineLen; 057 while(list.hasNext()) { 058 line=list.next(); 059 lineLen=metrics.stringWidth(line); 060 if(lineLen>with) { 061 reorganize(sb,list,new StringBuilder(line)); 062 break; 063 } 064 if(sb.length()>0)sb.append('\n'); 065 sb.append(line); 066 } 067 068 069 070 //int strLen = metrics.stringWidth(value); 071 return sb.toString();//metrics.stringWidth(value)+"-"+with+":"+value; 072 //return "StringUtil.reverse()"; 073 } 074 075 private void reorganize(StringBuilder sb, StringList list, StringBuilder rest) { 076 // fill rest 077 String item; 078 while(list.hasNext()) { 079 item=list.next(); 080 rest.append(list.delimiter()); 081 rest.append(item); 082 } 083 084 StringList words = ListUtil.toWordList(rest.toString()); 085 StringBuffer line=new StringBuffer(); 086 087 while(words.hasNext()) { 088 item=words.next(); 089 090 if(line.length()>0 && metrics.stringWidth(item.concat(" ").concat(line.toString()))>with) { 091 if(sb.length()>0) sb.append('\n'); 092 sb.append(line); 093 //print.out("line:"+line); 094 line=new StringBuffer(item); 095 } 096 else { 097 //item=words.next(); 098 if(line.length()>0)line.append(words.delimiter()==0?' ':words.delimiter()); 099 line.append(item); 100 } 101 } 102 if(line.length()>0){ 103 if(sb.length()>0) sb.append('\n'); 104 sb.append(line); 105 } 106 } 107}