package eu.lsem.bakalarka.model;
import org.krysalis.jcharts.chartData.DataSeries;
import org.krysalis.jcharts.chartData.AxisChartDataSet;
import org.krysalis.jcharts.chartData.ChartDataException;
import org.krysalis.jcharts.properties.*;
import org.krysalis.jcharts.types.ChartType;
import org.krysalis.jcharts.axisChart.AxisChart;
import org.krysalis.jcharts.encoders.PNGEncoder;
import java.awt.*;
import java.util.Arrays;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.File;
public class ColumnChart extends Chart {
private String xAxisTitle, yAxisTitle;
private double[][] data;
private String[] xAxisLabels, legendLabels;
public ColumnChart(String title, int width, int height, String xAxisTitle, String yAxisTitle, double[][] data, String[] xAxisLabels, String[] legendLabels) {
super(title, width, height);
this.xAxisTitle = xAxisTitle;
this.yAxisTitle = yAxisTitle;
this.data = data;
this.xAxisLabels = xAxisLabels;
this.legendLabels = legendLabels;
}
@Override
public void saveChart(File file) throws IOException, PropertyException, ChartDataException {
DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle, yAxisTitle, title);
ClusteredBarChartProperties clusteredBarChartProperties = new ClusteredBarChartProperties();
Color[] poleBarev = Arrays.copyOfRange(COLORS, 0, legendLabels.length);
AxisChartDataSet dataSet = new AxisChartDataSet(data, legendLabels, poleBarev, ChartType.BAR_CLUSTERED, clusteredBarChartProperties);
dataSeries.addIAxisPlotDataSet(dataSet);
ChartProperties chartProperties = new ChartProperties();
AxisProperties axisProperties = new AxisProperties();
LegendProperties legendProperties = new LegendProperties();
legendProperties.setNumColumns(1);
AxisChart axisChart = new AxisChart(dataSeries, chartProperties, axisProperties, legendProperties, width, height);
PNGEncoder.encode(axisChart, new FileOutputStream(file));
}
}