1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import lt.monarch.chart.chart2D.Chart2D;
import lt.monarch.chart.chart2D.series.PieLabelsLayouters;
import lt.monarch.chart.chart2D.series.PieSeries;
import lt.monarch.chart.engine.ChartObject;
import lt.monarch.chart.engine.SimpleHotSpotMap;
import lt.monarch.chart.legend.Legend;
import lt.monarch.chart.models.ChartDataModel;
import lt.monarch.chart.models.MetaDataModel;
import lt.monarch.chart.models.MetaDataType;
import lt.monarch.chart.style.Styles;
import lt.monarch.chart.style.enums.PieLabelAlignment;
import lt.monarch.chart.style.enums.PieLabelFormat;
import lt.monarch.chart.style.enums.PieLegendFormat;
import lt.monarch.chart.style.tags.SeriesPaintTags;
import lt.monarch.chart.swing.JChartPanel;
import lt.monarch.chart.view.LabeledChart;
public class Pie2DSeriesDemo extends JFrame {
private JChartPanel m_chartPanel;
public Pie2DSeriesDemo() {
setLayout(new BorderLayout());
initChart();
add(m_chartPanel, BorderLayout.CENTER);
}
private void initChart() {
// setting chart data model
ChartDataModel pieModel = new ChartDataModel();
pieModel.add(new Object[] { "Chevrolet", new Double(0.58) });
pieModel.add(new Object[] { "Dodge", new Double(0.58) });
pieModel.add(new Object[] { "Honda motorcycles", new Double(0.64) });
pieModel.add(new Object[] { "Hyundai", new Double(0.83) });
pieModel.add(new Object[] { "Harley Davidson", new Double(0.99) });
pieModel.add(new Object[] { "Nissan", new Double(1.05) });
pieModel.add(new Object[] { "Ford", new Double(1.08) });
pieModel.add(new Object[] { "Oreilly auto parts", new Double(1.24) });
pieModel.add(new Object[] { "Honda", new Double(1.35) });
pieModel.add(new Object[] { "Toyota", new Double(1.44) });
// setting chart style
MetaDataModel meta1 = new MetaDataModel();
// creating style objects
Styles<SeriesPaintTags> style1 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style2 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style3 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style4 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style5 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style6 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style7 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style8 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style9 = new Styles<SeriesPaintTags>();
Styles<SeriesPaintTags> style10 = new Styles<SeriesPaintTags>();
// setting background color for each slice
style1.getPaintStyle().setBackground(new Color(251, 178, 47));
style2.getPaintStyle().setBackground(new Color(143, 60, 168));
style3.getPaintStyle().setBackground(new Color(49, 119, 31));
style4.getPaintStyle().setBackground(new Color(219, 67, 47));
style5.getPaintStyle().setBackground(new Color(223, 95, 32));
style6.getPaintStyle().setBackground(new Color(218, 24, 121));
style7.getPaintStyle().setBackground(new Color(75, 146, 188));
style8.getPaintStyle().setBackground(new Color(216, 205, 1));
style9.getPaintStyle().setBackground(new Color(255, 56, 35));
style10.getPaintStyle().setBackground(new Color(144, 193, 13));
// setting foreground color for each slice
style1.getPaintStyle().setForeground(new Color(251, 178, 47));
style2.getPaintStyle().setForeground(new Color(143, 60, 168));
style3.getPaintStyle().setForeground(new Color(49, 119, 31));
style4.getPaintStyle().setForeground(new Color(219, 67, 47));
style5.getPaintStyle().setForeground(new Color(223, 95, 32));
style6.getPaintStyle().setForeground(new Color(218, 24, 121));
style7.getPaintStyle().setForeground(new Color(75, 146, 188));
style8.getPaintStyle().setForeground(new Color(216, 205, 1));
style9.getPaintStyle().setForeground(new Color(255, 56, 35));
style10.getPaintStyle().setForeground(new Color(144, 193, 13));
// setting text font for each slice
style1.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style2.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style3.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style4.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style5.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style6.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style7.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style8.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style9.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
style10.getTextStyle().setFont(new Font("Arial", Font.BOLD, 12));
// adding styles to meta data
meta1.setData(MetaDataType.STYLE, 0, style1);
meta1.setData(MetaDataType.STYLE, 1, style2);
meta1.setData(MetaDataType.STYLE, 2, style3);
meta1.setData(MetaDataType.STYLE, 3, style4);
meta1.setData(MetaDataType.STYLE, 4, style5);
meta1.setData(MetaDataType.STYLE, 5, style6);
meta1.setData(MetaDataType.STYLE, 6, style7);
meta1.setData(MetaDataType.STYLE, 7, style8);
meta1.setData(MetaDataType.STYLE, 8, style9);
meta1.setData(MetaDataType.STYLE, 9, style10);
// setting explode for slices
// second parameter sets for which slice explode will be set
// third parameter - sets how much explode there should be
meta1.setData(MetaDataType.PIE_EXPLODE, 0, new Double(0.05));
meta1.setData(MetaDataType.PIE_EXPLODE, 1, new Double(0.05));
meta1.setData(MetaDataType.PIE_EXPLODE, 3, new Double(0.05));
meta1.setData(MetaDataType.PIE_EXPLODE, 4, new Double(0.05));
// setting series
PieSeries pie = new PieSeries(pieModel, meta1);
// setting labels
pie
.setLabelLayouter(PieLabelsLayouters.LABELS_OUTSIDE_WITH_STRAIGHT_LINE);
pie.setLabelAlignment(PieLabelAlignment.LEFT_RIGHT);
pie.setLabelFormat(PieLabelFormat.SLICE_NAME_LABELS);
// setting legend format
pie.setLegendFormat(PieLegendFormat.CUSTOM_LABELS);
pie.setLegendFormatPattern("%3$s - %2$.1f%%");
// setting chart
Chart2D chart = new Chart2D();
chart.setObjects(new ChartObject[] { pie });
LabeledChart m_chart = new LabeledChart(chart);
// setting title
m_chart
.setTitle("Top 10 Search Terms for Automotive Manufacturers in 2009 March");
// setting legend
Legend legend = new Legend(chart);
m_chart.setRightView(legend);
// setting JChartPanel
m_chartPanel = new JChartPanel(m_chart);
m_chartPanel.setHotSpotMap(new SimpleHotSpotMap());
}
public static void main(String[] args) {
Pie2DSeriesDemo frame = new Pie2DSeriesDemo();
frame.setSize(new Dimension(660, 400));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setTitle("Top 10 Search Terms for Automotive Manufacturers in 2009 March");
frame.setVisible(true);
}
} |