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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | import java.awt.*;
import java.util.ArrayList;
import javax.swing.JFrame;
import lt.monarch.chart.chart2D.Chart2D;
import lt.monarch.chart.chart2D.Grid;
import lt.monarch.chart.chart2D.axis.Axis2DX;
import lt.monarch.chart.chart2D.axis.Axis2DY;
import lt.monarch.chart.chart2D.engine.PlaneMapper2D;
import lt.monarch.chart.chart2D.series.BarSeries;
import lt.monarch.chart.engine.ChartObject;
import lt.monarch.chart.engine.SimpleHotSpotMap;
import lt.monarch.chart.legend.Legend;
import lt.monarch.chart.mapper.LabelAxisMapper;
import lt.monarch.chart.mapper.MathAxisMapper;
import lt.monarch.chart.models.ChartDataModel;
import lt.monarch.chart.models.DataModelUtil;
import lt.monarch.chart.models.StackedDataModel;
import lt.monarch.chart.swing.JChartPanel;
import lt.monarch.chart.view.LabeledChart;
public class StackedDataModelsDemo extends JFrame {
/**
* Container for chart
*/
private JChartPanel m_chartPanel;
/**
* Constructor
*/
public StackedDataModelsDemo() {
setLayout(new BorderLayout());
initChart();
add(m_chartPanel, BorderLayout.CENTER);
}
/**
* Method for setting chart
*/
private void initChart() {
/*
* Creating data models
*/
ArrayList<Object[]> list = new ArrayList<Object[]>();
list.add(new Object[] { "2006", new Double(5020.6) });
list.add(new Object[] { "2007", new Double(5857.0) });
list.add(new Object[] { "2008", new Double(7084.4) });
ChartDataModel dataModel1 = new ChartDataModel(DataModelUtil
.convert(list));
list = new ArrayList<Object[]>();
list.add(new Object[] { "2006", new Double(5865.0) });
list.add(new Object[] { "2007", new Double(7062.3) });
list.add(new Object[] { "2008", new Double(8284.4) });
ChartDataModel dataModel2 = new ChartDataModel(DataModelUtil
.convert(list));
list = new ArrayList<Object[]>();
list.add(new Object[] { "2006", new Double(6431.3) });
list.add(new Object[] { "2007", new Double(7586.6) });
list.add(new Object[] { "2008", new Double(8646.5) });
ChartDataModel dataModel3 = new ChartDataModel(DataModelUtil
.convert(list));
list = new ArrayList<Object[]>();
list.add(new Object[] { "2006", new Double(6661.5) });
list.add(new Object[] { "2007", new Double(7917.0) });
list.add(new Object[] { "2008", new Double(8277.0) });
ChartDataModel dataModel4 = new ChartDataModel(DataModelUtil
.convert(list));
/*
* Creating stacked data models
*/
StackedDataModel dm1 = new StackedDataModel(dataModel1);
StackedDataModel dm2 = dm1.setStackedModel(dataModel2);
StackedDataModel dm3 = dm2.setStackedModel(dataModel3);
StackedDataModel dm4 = dm3.setStackedModel(dataModel4);
/*
* Creating mappers
*/
String labels[] = { "2006", "2007", "2008" };
LabelAxisMapper xMapper = new LabelAxisMapper(labels);
MathAxisMapper yMapper = new MathAxisMapper(0, 35000);
/*
* Creating Axes
*/
Axis2DX axisX = new Axis2DX(xMapper);
axisX.setTitle("Year");
axisX.getArrowSettings().setDrawArrow(true);
Axis2DY axisY = new Axis2DY(yMapper);
axisY.setTitle("Millions of Euro");
axisY.getArrowSettings().setDrawArrow(true);
/*
* Creating and setting series
*/
BarSeries bar1 = new BarSeries(dm1, xMapper, yMapper);
bar1.setName("1 Quater");
bar1.getPaintStyle().setBackground(new Color(51, 102, 0,200));
bar1.style.setForeground(null);
bar1.getPaintStyle().setStroke(new BasicStroke(1f));
BarSeries bar2 = new BarSeries(dm2, xMapper, yMapper);
bar2.setName("2 Quater");
bar2.getPaintStyle().setStroke(new BasicStroke(1f));
bar2.getPaintStyle().setBackground(new Color(251, 178, 47,200));
bar2.style.setForeground(null);
BarSeries bar3 = new BarSeries(dm3, xMapper, yMapper);
bar3.setName("3 Quarter");
bar3.style.setBackground(new Color(219, 67, 47,200));
bar3.getPaintStyle().setStroke(new BasicStroke(1f));
bar3.style.setForeground(null);
BarSeries bar4 = new BarSeries(dm4, xMapper, yMapper);
bar4.setName("4 Quater");
bar4.getPaintStyle().setStroke(new BasicStroke(1f));
bar4.style.setForeground(null);
bar4.getPaintStyle().setBackground(new Color(75, 146, 188,200));
/*
* Creating grid
*/
Grid grid = new Grid(new PlaneMapper2D(), null, yMapper);
/*
* Creating chart
*/
Chart2D chart = new Chart2D();
chart.setObjects(new ChartObject[] { grid, bar1, bar2, bar3, bar4 });
chart.setXAxis(axisX);
chart.setYAxis(axisY);
/*
* Creating labeled chart and setting title
*/
LabeledChart m_chart = new LabeledChart(chart);
m_chart
.setTitle("GPD of Lithuania by Quater and Year in Millions of Euro");
/*
* Creating legend
*/
Legend legend = new Legend(chart);
legend.setMaxColumns(4);
m_chart.setBottomView(legend);
m_chartPanel = new JChartPanel(m_chart);
m_chartPanel.setHotSpotMap(new SimpleHotSpotMap());
}
/**
* Initialization the chart
*/
public static void main(String[] args) {
StackedDataModelsDemo frame = new StackedDataModelsDemo();
frame.setSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setTitle("GPD of Lithuania by Quater and Year in Millions of Euro");
frame.setVisible(true);
}
} |