package eu.lsem.bakalarka.model;
import org.krysalis.jcharts.chartData.PieChartDataSet;
import org.krysalis.jcharts.chartData.ChartDataException;
import org.krysalis.jcharts.properties.PieChart2DProperties;
import org.krysalis.jcharts.properties.LegendProperties;
import org.krysalis.jcharts.properties.ChartProperties;
import org.krysalis.jcharts.properties.PropertyException;
import org.krysalis.jcharts.nonAxisChart.PieChart2D;
import org.krysalis.jcharts.encoders.PNGEncoder;
import org.krysalis.jcharts.types.PieLabelType;
import java.awt.*;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.File;
import java.util.Arrays;
public class PieChart extends Chart {
private String[] legendLabels;
private double[] data;
private boolean showChartLabels;
public PieChart(String title, int width, int height, String[] legendLabels, double[] data, boolean showChartLabels) {
super(title, width, height);
this.legendLabels = legendLabels;
this.data = data;
this.showChartLabels = showChartLabels;
}
@Override
public void saveChart(File file) throws IOException, PropertyException, ChartDataException {
Color[] poleBarev = Arrays.copyOfRange(COLORS, 0, legendLabels.length);
PieChart2DProperties properties = new PieChart2DProperties();
if (showChartLabels)
properties.setPieLabelType(PieLabelType.VALUE_LABELS);
PieChartDataSet dataset = new PieChartDataSet(title, data, legendLabels, poleBarev, properties);
LegendProperties legend = new LegendProperties();
legend.setNumColumns(1);
ChartProperties chartProperties = new ChartProperties();
chartProperties.setUseAntiAliasing(true);
PieChart2D chart = new PieChart2D(dataset, legend, chartProperties, width, height);
PNGEncoder.encode(chart, new FileOutputStream(file));
}
}