Android : the graph is not sync with the pointstyle in CubicLineChart achartengine - java

I am trying to create a curve graph using the function in the library CubicLineChart Achartengine, but the point with a line is not fused as shown below:
I added more smooth in my chart, but the line with points farther away, this is my code :
onPaint(){
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeries mCurrentSeries = null;
XYSeriesRenderer mCurrentRenderer;
String [] bulan= {"jan", "feb", "mar", "apr", "jun", "jul", "agt", "sep", "okt", "nov", "des"};
int[] colors = new int[] { Color.RED, Color.GREEN};
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.CIRCLE};
// mDataset.removeSeries(mCurrentSeries);
mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setAxisTitleTextSize(14);
mRenderer.setChartTitleTextSize(14);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 0, 15, 10, 0 });
mRenderer.setZoomButtonsVisible(false);//untuk hilangin zoomnya
mRenderer.setPanEnabled(false, false);
mRenderer.setZoomEnabled(false, false);
mRenderer.setPointSize(2f);
mRenderer.setMarginsColor(Color.TRANSPARENT);
mRenderer.setShowGrid(true);
String seriesTitle = "";
XYSeries series = new XYSeries(seriesTitle);
mDataset.addSeries(series);
mCurrentSeries = series;
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setFillBelowLine(true);
renderer.setFillBelowLineColor(Color.TRANSPARENT);
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.CIRCLE);
mRenderer.addSeriesRenderer(renderer);
mRenderer.setInScroll(true);
mRenderer.setShowLegend(false);
mRenderer.setShowGridX(true);
mRenderer.setShowGridY(false);
mRenderer.setXLabels(bulan.length);
mRenderer.setYLabels(5);
mRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));//hilangin background hitam disekeliling grafik
renderer.setFillPoints(true);
mCurrentRenderer = renderer;
for (int i = 0; i < bulan.length; i++) {
mCurrentSeries.add(i,jumlah[i]);
}
if (mChartView != null) {
piechart.removeView(mChartView);
}
mChartView = ChartFactory.getCubeLineChartView(applicationContext, mDataset, mRenderer, 0.17f);
piechart.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
pw_kn.showAtLocation(popUpKN, Gravity.CENTER, 0, 0);
}
is there something wrong with my code?

Related

Second Line in an Apache-POI chart with seperate axis

Hi this code taken from the answer here is working as expected, but I want exactly the same Chart but in an Excel-Sheet
package eu.flexsolution.task.excel;
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
public class TEst {
public static void main(String[] args) throws Exception {
try (XWPFDocument document = new XWPFDocument()) {
// create the data
String[] categories = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Double[] values1 = new Double[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d };
Double[] values2 = new Double[] { 200d, 300d, 400d, 500d, 600d, 700d, 800d, 900d, 1000d };
// create the chart
XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);
// create data sources
int numOfPoints = categories.length;
String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
String valuesDataRange1 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
XDDFNumericalDataSource<Double> valuesData1 = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange1,
1);
XDDFNumericalDataSource<Double> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2,
2);
// first line chart
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFChartData.Series series = data.addSeries(categoriesData, valuesData1);
chart.plot(data);
solidLineSeries(data, 0, PresetColor.BLUE);
// second line chart
// bottom axis must be there but must not be visible
bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setVisible(false);
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
// set correct cross axis
bottomAxis.crossAxis(rightAxis);
rightAxis.crossAxis(bottomAxis);
data = chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
series = data.addSeries(categoriesData, valuesData2);
chart.plot(data);
// correct the id and order, must not be 0 again because there is one line
// series already
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getIdx().setVal(1);
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getOrder().setVal(1);
solidLineSeries(data, 0, PresetColor.RED);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("CreateWordXDDFChart.docx")) {
document.write(fileOut);
}
}
}
private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
XDDFChartData.Series series = data.getSeries().get(index);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties(line);
series.setShapeProperties(properties);
}
}
So I modified the code like this to get an XLSX document, but the Chart isn't the same
package eu.flexsolution.task.excel;
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TEst {
public static void main(String[] args) throws Exception {
try (XSSFWorkbook document = new XSSFWorkbook()) {
XSSFSheet chartSheet = document.createSheet("chart");
// create the data
String[] categories = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Double[] values1 = new Double[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d };
Double[] values2 = new Double[] { 200d, 300d, 400d, 500d, 600d, 700d, 800d, 900d, 1000d };
// create the chart
XSSFDrawing drawing = chartSheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 26, 40);
XDDFChart chart = drawing.createChart(anchor);
// create data sources
int numOfPoints = categories.length;
String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
String valuesDataRange1 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
String valuesDataRange2 = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
XDDFNumericalDataSource<Double> valuesData1 = XDDFDataSourcesFactory.fromArray(values1, valuesDataRange1,
1);
XDDFNumericalDataSource<Double> valuesData2 = XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2,
2);
// first line chart
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFChartData.Series series = data.addSeries(categoriesData, valuesData1);
chart.plot(data);
solidLineSeries(data, 0, PresetColor.BLUE);
// second line chart
// bottom axis must be there but must not be visible
bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setVisible(false);
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
// set correct cross axis
bottomAxis.crossAxis(rightAxis);
rightAxis.crossAxis(bottomAxis);
data = chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
series = data.addSeries(categoriesData, valuesData2);
chart.plot(data);
// correct the id and order, must not be 0 again because there is one line
// series already
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getIdx().setVal(1);
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getOrder().setVal(1);
solidLineSeries(data, 0, PresetColor.RED);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("CreateWordXDDFChart.xlsx")) {
document.write(fileOut);
}
}
}
private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
XDDFChartData.Series series = data.getSeries().get(index);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties(line);
series.setShapeProperties(properties);
}
}
The problem is that for a Word (XWPF) chart, the data are stored in a Excel workbook which is embedded in the Word file. There the data can be given as arrays and handled via XDDFDataSourcesFactory.fromArray. This then fills the embedded Excel data sheet.
But for a Excel(XSSF) chart the data needs to be in a Excel data sheet. Of course Excel will not embedding a Excel sheet in it's files as it has worksheets already. So for Excel the data needs to be in a worksheet and needs to be handled via XDDFDataSourcesFactory.fromStringCellRange or XDDFDataSourcesFactory.fromNumericCellRange then.
Complete example which creates the Excel XSSFChart:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelXDDFChart {
public static void main(String[] args) throws Exception {
try (XSSFWorkbook document = new XSSFWorkbook()) {
XSSFSheet chartSheet = document.createSheet("chart");
XSSFSheet dataSheet = document.createSheet("data");
// create the data
String[] categories = new String[] { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9" };
Double[] values1 = new Double[] { 1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d };
Double[] values2 = new Double[] { 200d, 300d, 400d, 500d, 600d, 700d, 800d, 900d, 1000d };
int r = 0;
for (String cat : categories) {
dataSheet.createRow(r).createCell(0).setCellValue(cat);
dataSheet.getRow(r).createCell(1).setCellValue(values1[r]);
dataSheet.getRow(r).createCell(2).setCellValue(values2[r]);
r++;
}
// create the chart
XSSFDrawing drawing = chartSheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 26, 40);
XDDFChart chart = drawing.createChart(anchor);
// create data sources
int numOfPoints = categories.length;
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromStringCellRange(dataSheet, new CellRangeAddress(0, numOfPoints-1, 0, 0));
XDDFNumericalDataSource<Double> valuesData1 = XDDFDataSourcesFactory.fromNumericCellRange(dataSheet, new CellRangeAddress(0, numOfPoints-1, 1, 1));
XDDFNumericalDataSource<Double> valuesData2 = XDDFDataSourcesFactory.fromNumericCellRange(dataSheet, new CellRangeAddress(0, numOfPoints-1, 2, 2));
// first line chart
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFChartData.Series series = data.addSeries(categoriesData, valuesData1);
chart.plot(data);
solidLineSeries(data, 0, PresetColor.BLUE);
// second line chart
// bottom axis must be there but must not be visible
bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setVisible(false);
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setCrosses(AxisCrosses.MAX);
// set correct cross axis
bottomAxis.crossAxis(rightAxis);
rightAxis.crossAxis(bottomAxis);
data = chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
series = data.addSeries(categoriesData, valuesData2);
chart.plot(data);
// correct the id and order, must not be 0 again because there is one line
// series already
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getIdx().setVal(1);
chart.getCTChart().getPlotArea().getLineChartArray(1).getSerArray(0).getOrder().setVal(1);
solidLineSeries(data, 0, PresetColor.RED);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("CreateExcelXDDFChart.xlsx")) {
document.write(fileOut);
}
}
}
private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
//XDDFChartData.Series series = data.getSeries().get(index);
XDDFChartData.Series series = data.getSeries(index);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties(line);
series.setShapeProperties(properties);
}
}
Works using current apache poi 4.1.2.

How to save multiple graphics in jfreechart?

Thank you very much for all the help you always give.
I have to save the following image
but when I use the Jfreechart utility (ChartUtils.saveChartAsPNG) to save I can only save each graph separately and not the complete one.
And when I use the (BufferedImage) utility, it stores in very bad quality, many blurry pixels, poor resolution.
What other alternative can I use?
This is my code:
String direccionTXTlinea = "\\\\172.16.40.10\\Geodesia\\volcanes\\lineas\\Maule_PUEL-MAU2.txt";
LectorTXT lectorTXT = new LectorTXT();
lectorTXT.leerTXTlinea(direccionTXTlinea);
TransformarTXT transformarTXT = new TransformarTXT();
transformarTXT.transformarDatosLinea(lectorTXT.getListaLinea());
GeneradorTimeSerie timeSerieTXT = new GeneradorTimeSerie();
timeSerieTXT.setTransformarTXT(transformarTXT);
timeSerieTXT.crearTimeSerieTXT();
TimeSeriesCollection UNO = new TimeSeriesCollection();
TimeSeriesCollection DOS = new TimeSeriesCollection();
TimeSeriesCollection TRES = new TimeSeriesCollection();
UNO.addSeries(timeSerieTXT.getSerieUNOmasERROR());
UNO.addSeries(timeSerieTXT.getSerieUNO());
UNO.addSeries(timeSerieTXT.getSerieUNOmenosERROR());
DOS.addSeries(timeSerieTXT.getSerieDOSmasERROR());
DOS.addSeries(timeSerieTXT.getSerieDOS());
DOS.addSeries(timeSerieTXT.getSerieDOSmenosERROR());
TRES.addSeries(timeSerieTXT.getSerieTRESmasERROR());
TRES.addSeries(timeSerieTXT.getSerieTRES());
TRES.addSeries(timeSerieTXT.getSerieTRESmenosERROR());
XYDataset datasetUNO = UNO;
XYDataset datasetDOS = DOS;
NumberAxis rangeAxis1 = new NumberAxis("L.Linea cm");
rangeAxis1.setAutoRangeIncludesZero(false);
XYItemRenderer renderer1 = new StandardXYItemRenderer();
renderer1.setSeriesPaint(0, Color.decode("#C1ECFF"));
renderer1.setSeriesPaint(2, Color.decode("#C1ECFF"));
renderer1.setSeriesPaint(1, Color.decode("#00067F"));
XYPlot subplot1 = new XYPlot(datasetUNO, null, rangeAxis1, renderer1);
NumberAxis rangeAxis2 = new NumberAxis("D.Vertical cm 2");
rangeAxis2.setAutoRangeIncludesZero(false);
XYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setSeriesPaint(0, Color.decode("#FF8B8A"));
renderer2.setSeriesPaint(2, Color.decode("#FF8B8A"));
renderer2.setSeriesPaint(1, Color.decode("#FF0800"));
XYPlot subplot2 = new XYPlot(datasetDOS, null, rangeAxis2, renderer2);
ValueAxis domainAxis = new DateAxis();
CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
plot.setDomainAxis(domainAxis);
plot.add(subplot1, 1);
plot.add(subplot2, 1);
plot.setOrientation(PlotOrientation.VERTICAL);
String Estacion1 = "\\\\172.16.40.10\\Geodesia\\volcanes\\zona2\\Maule\\Maule_mau2.txt";
String Estacion2 = "\\\\172.16.40.10\\Geodesia\\volcanes\\zona2\\Maule\\Maule_nieb.txt";
LectorTXT lectorTXTEstacion1 = new LectorTXT();
LectorTXT lectorTXTEstacion2 = new LectorTXT();
lectorTXTEstacion1.leerTXTlinea(Estacion1);
lectorTXTEstacion2.leerTXTlinea(Estacion2);
TransformarTXT transformarTXTestacion1 = new TransformarTXT();
transformarTXTestacion1.transformarDatosEstacion(lectorTXTEstacion1.getListaLinea());
TransformarTXT transformarTXTestacion2 = new TransformarTXT();
transformarTXTestacion2.transformarDatosEstacion(lectorTXTEstacion2.getListaLinea());
GeneradorTimeSerie timeSerieTXT1 = new GeneradorTimeSerie();
timeSerieTXT1.setTransformarTXT(transformarTXTestacion1);
GeneradorTimeSerie timeSerieTXT2 = new GeneradorTimeSerie();
timeSerieTXT2.setTransformarTXT(transformarTXTestacion2);
timeSerieTXT1.crearTimeSerieTXT();
timeSerieTXT2.crearTimeSerieTXT();
TimeSeriesCollection UNO1 = new TimeSeriesCollection();
TimeSeriesCollection DOS1 = new TimeSeriesCollection();
TimeSeriesCollection TRES1 = new TimeSeriesCollection();
TimeSeriesCollection UNO2 = new TimeSeriesCollection();
TimeSeriesCollection DOS2 = new TimeSeriesCollection();
TimeSeriesCollection TRES2 = new TimeSeriesCollection();
UNO1.addSeries(timeSerieTXT1.getSerieDOSmasERROR());
UNO1.addSeries(timeSerieTXT1.getSerieDOS());
UNO1.addSeries(timeSerieTXT1.getSerieDOSmenosERROR());
DOS1.addSeries(timeSerieTXT1.getSerieDOSmasERROR());
DOS1.addSeries(timeSerieTXT1.getSerieDOS());
DOS1.addSeries(timeSerieTXT1.getSerieDOSmenosERROR());
TRES1.addSeries(timeSerieTXT1.getSerieTRESmasERROR());
TRES1.addSeries(timeSerieTXT1.getSerieTRES());
TRES1.addSeries(timeSerieTXT1.getSerieTRESmenosERROR());
UNO2.addSeries(timeSerieTXT2.getSerieDOSmasERROR());
UNO2.addSeries(timeSerieTXT2.getSerieDOS());
UNO2.addSeries(timeSerieTXT2.getSerieDOSmenosERROR());
DOS2.addSeries(timeSerieTXT2.getSerieDOSmasERROR());
DOS2.addSeries(timeSerieTXT2.getSerieDOS());
DOS2.addSeries(timeSerieTXT2.getSerieDOSmenosERROR());
TRES2.addSeries(timeSerieTXT2.getSerieTRESmasERROR());
TRES2.addSeries(timeSerieTXT2.getSerieTRES());
TRES2.addSeries(timeSerieTXT2.getSerieTRESmenosERROR());
XYDataset datasetUNO1 = UNO1;
XYDataset datasetDOS1 = DOS1;
XYDataset datasetTRES1 = TRES1;
XYDataset datasetUNO2 = UNO2;
XYDataset datasetDOS2 = DOS2;
XYDataset datasetTRES2 = TRES2;
NumberAxis rangeAxis11 = new NumberAxis("Norte cm");
rangeAxis11.setAutoRangeIncludesZero(false);
XYItemRenderer renderer11 = new StandardXYItemRenderer();
renderer11.setSeriesPaint(0, Color.decode("#C1ECFF"));
renderer11.setSeriesPaint(2, Color.decode("#C1ECFF"));
renderer11.setSeriesPaint(1, Color.decode("#00067F"));
XYPlot subplot11 = new XYPlot(datasetUNO1, null, rangeAxis11, renderer11);
NumberAxis rangeAxis21 = new NumberAxis("Este cm");
rangeAxis21.setAutoRangeIncludesZero(false);
XYItemRenderer renderer21 = new StandardXYItemRenderer();
renderer21.setSeriesPaint(0, Color.decode("#A8FFA4"));
renderer21.setSeriesPaint(2, Color.decode("#A8FFA4"));
renderer21.setSeriesPaint(1, Color.decode("#15FF1B"));
XYPlot subplot21 = new XYPlot(datasetDOS1, null, rangeAxis21, renderer21);
NumberAxis rangeAxis31 = new NumberAxis("Vertical cm");
rangeAxis31.setAutoRangeIncludesZero(false);
XYItemRenderer renderer31 = new StandardXYItemRenderer();
renderer31.setSeriesPaint(0, Color.decode("#FF8B8A"));
renderer31.setSeriesPaint(2, Color.decode("#FF8B8A"));
renderer31.setSeriesPaint(1, Color.decode("#FF0800"));
XYPlot subplot31 = new XYPlot(datasetTRES1, null, rangeAxis31, renderer31);
ValueAxis domainAxis1 = new DateAxis();
CombinedDomainXYPlot plot1 = new CombinedDomainXYPlot();
plot1.setDomainAxis(domainAxis1);
plot1.add(subplot11, 1);
plot1.add(subplot21, 1);
plot1.add(subplot31, 1);
plot1.setOrientation(PlotOrientation.VERTICAL);
NumberAxis rangeAxis12 = new NumberAxis("Norte cm");
rangeAxis12.setAutoRangeIncludesZero(false);
XYItemRenderer renderer12 = new StandardXYItemRenderer();
renderer12.setSeriesPaint(0, Color.decode("#C1ECFF"));
renderer12.setSeriesPaint(2, Color.decode("#C1ECFF"));
renderer12.setSeriesPaint(1, Color.decode("#00067F"));
XYPlot subplot12 = new XYPlot(datasetUNO2, null, rangeAxis12, renderer12);
NumberAxis rangeAxis22 = new NumberAxis("Este cm");
rangeAxis22.setAutoRangeIncludesZero(false);
XYItemRenderer renderer22 = new StandardXYItemRenderer();
renderer22.setSeriesPaint(0, Color.decode("#A8FFA4"));
renderer22.setSeriesPaint(2, Color.decode("#A8FFA4"));
renderer22.setSeriesPaint(1, Color.decode("#15FF1B"));
XYPlot subplot22 = new XYPlot(datasetDOS2, null, rangeAxis22, renderer22);
NumberAxis rangeAxis32 = new NumberAxis("Vertical cm");
rangeAxis32.setAutoRangeIncludesZero(false);
XYItemRenderer renderer32 = new StandardXYItemRenderer();
renderer32.setSeriesPaint(0, Color.decode("#FF8B8A"));
renderer32.setSeriesPaint(2, Color.decode("#FF8B8A"));
renderer32.setSeriesPaint(1, Color.decode("#FF0800"));
XYPlot subplot32 = new XYPlot(datasetTRES2, null, rangeAxis32, renderer32);
ValueAxis domainAxis2 = new DateAxis();
CombinedDomainXYPlot plot2 = new CombinedDomainXYPlot();
plot2.setDomainAxis(domainAxis2);
plot2.add(subplot12, 1);
plot2.add(subplot22, 1);
plot2.add(subplot32, 1);
plot2.setOrientation(PlotOrientation.VERTICAL);
JFreeChart a = new JFreeChart("Linea: ",JFreeChart.DEFAULT_TITLE_FONT, plot, false);
JFreeChart b = new JFreeChart("Estacion GPS: ",JFreeChart.DEFAULT_TITLE_FONT, plot1, false);
JFreeChart c = new JFreeChart("Estacion GPS: ",JFreeChart.DEFAULT_TITLE_FONT, plot2, false);
ChartPanel panel = new ChartPanel(a, true, true, true, true, true);
ChartPanel panel2 = new ChartPanel(b, true, true, true, true, true);
ChartPanel panel3 = new ChartPanel(c, true, true, true, true, true);
Dimension dmnsn = new Dimension(950, 500);
Dimension dmnsn2 = new Dimension(1900, 500);
panel.setMaximumDrawHeight(500);
panel.setMinimumDrawHeight(450);
panel.setMaximumDrawWidth(1900);
panel.setMinimumDrawWidth(950);
panel.setMinimumSize(dmnsn2);
panel2.setMinimumSize(dmnsn);
panel3.setMinimumSize(dmnsn);
JFrame frame = new JFrame("VOLCAN");
JPanel asd = new JPanel();
asd.setLayout(new GridBagLayout());
GridBagConstraints csd = new GridBagConstraints();
JLabel volcan = new JLabel("el volcan");
csd.gridx = 0;
csd.gridy = 0;
csd.gridwidth = 2;
asd.add(volcan, csd);
frame.add(asd);
csd.gridx = 0;
csd.gridy = 1;
csd.gridwidth = 1;
asd.add(panel3, csd);
frame.add(asd);
csd.gridx = 1;
csd.gridy = 1;
csd.gridwidth = 1;
asd.add(panel2, csd);
frame.add(asd);
csd.gridx = 0;
csd.gridy = 2;
csd.gridwidth =2;
asd.add(panel, csd);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

Inappropriate view of x-axis labels in jfreecharts.

I have an issue with my jfreechart graph. Actually at x-axis the value are in big numbers (i-e 1500-2000) and the amounts of these values are also too large (i.e in significant notation).So the chart is just showing a dashed line instead of values at x-axis.
public static byte[] createImageByXyChart(String str_chartLabel,
String str_xAxisLabel, String str_yAxisLabel,
String str_fileFormat, Integer i_height, Integer i_width,
List<double[]> co_ordinates) throws IOException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < co_ordinates.size(); i++) {
double[] xy_coOrdinates = co_ordinates.get(i);
double xCoordiate = xy_coOrdinates[0];
double yCoordiate = xy_coOrdinates[1];
dataset.setValue(yCoordiate, str_chartLabel, "" + xCoordiate);
}
JFreeChart chart =
ChartFactory.createLineChart(str_chartLabel,str_xAxisLabel,
str_yAxisLabel, dataset);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
plot.setRangePannable(true);
plot.setRangeGridlinesVisible(true);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlineStroke(new BasicStroke(1.0f));
plot.setRangeGridlineStroke(new BasicStroke(1.0f));
plot.setDomainGridlinePaint(Color.GRAY.brighter());
plot.setRangeGridlinePaint(Color.GRAY.brighter());
ChartPanel panel = new ChartPanel(chart);
JFrame frame = new JFrame("Show chart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(600, 600);
frame.setVisible(true);
return null;
}
public static void main(String[] arg) throws Exception {
System.out.println("########################### ");
File fileData = new File("curve.txt");
BufferedReader br = new BufferedReader(new FileReader(fileData));
List<double[]> co_ordinates = new ArrayList<double[]>();
String line = null;
while ((line = br.readLine()) != null) {
String[] abcd = line.split(",");
double[] xy_cor = new double[2];
NumberFormat df = new DecimalFormat("######.####");
xy_cor[0] = Double.parseDouble(df.format(Double
.parseDouble(abcd[0])));
System.out.println(xy_cor[0]);
xy_cor[1] = Double.parseDouble(abcd[1]);
System.out.println(Arrays.toString(xy_cor));
co_ordinates.add(xy_cor);
}
br.close();
String str_chartLaber = "Curve";
String str_xAxisLabel = "Time(s)";
String str_yAxisLaber = "Accelaration (mm_per__s_2)";
String str_fileFormate = ImageFormats.JPEG;
Integer heigth = 1800;
Integer width = 1080;
byte[] image_bytes = ImageUtils.createImageByXyChart(str_chartLaber,
str_xAxisLabel, str_yAxisLaber, str_fileFormate, heigth, width,
co_ordinates);
}
the input data is that
Heading
i use
XYDataset dataset ;
instead of
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
The XYDataset enable auto interval where DefaultCategoryDataset do not.I don't know the reason but it is.

AChartEngine Plotting, ArrayList indexing

I am new to android, and I have to create an app that plots data. One set of data is about 7000 data point, and I've been using AChartEngine. I am using an ArrayList to hold the data.
ArrayList<Double> forehead = new ArrayList<Double>();
private XYMultipleSeriesDataset getforeDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYSeries firstSeries = new XYSeries("Forehead");
for (int i = 0; i < forehead.size(); i++)
firstSeries.add(i, forehead.get(i));
dataset.addSeries(firstSeries);
return dataset;
this is where I add data into the ArrayList, a sample string that would be read in is:
"02888 0 096 098 080"
private void getIntegerArray(ArrayList<String> stringArray) {
for (String s:stringArray){
String[] str = s.split("\t");
forehead.add(Double.parseDouble(str[3]));
}
}
private XYMultipleSeriesRenderer getforeRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setMargins(new int[] { 0, 0, 0, 0 });
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.BLUE);
r.setPointStyle(PointStyle.POINT);
r.setFillPoints(false);
renderer.addSeriesRenderer(r);
return renderer;
}
plotting
mChartView2 = ChartFactory.getLineChartView(getActivity(), getforeDataset(),
getforeRenderer());
layout2.addView(mChartView2);
Here is my application and no data is displayed, how can I display the 'forehead' ArrayList?
http://imgur.com/aYyDVnL
Use this
String[] str = s.split(" ");
instead of
String[] str = s.split("\t");
I think you are not getting the values as you want.

JFreeChart XYSeries as Strings

im working with jfreechart and try to make a XYLineChart which is working very well.
My problem is, that the y label are double values and i need strings.
My Code:
DefaultXYDataset result = new DefaultXYDataset();
XYSeries series1 = new XYSeries("Words");
series1.add(0, 0.3);
series1.add(1, 0.5);
series1.add(2, 0.6);
series1.add(3, 0.3);
series1.add(4, 0.2);
series1.add(5, 1);
result.addSeries(getTitle(), series1.toArray());
I want something like:
XYSeries series1 = new XYSeries("Words");
series1.add("word 1", 0.3);
series1.add("word 2", 0.5);
...
The updated code using Symbol-Axis:
private void test2() {
XYDataset dataset = createDataset2();
JFreeChart chart = createChart2(dataset, "NN");
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 250));
panel_visualize.add(chartPanel);
}
private DefaultXYDataset createDataset2()
{
DefaultXYDataset result = new DefaultXYDataset();
XYSeries series1 = new XYSeries("Words");
series1.add(0.3, 0);
series1.add(0.5, 1);
series1.add(0.6, 2);
series1.add(0.3, 3);
series1.add(0.2, 4);
result.addSeries(getTitle(), series1.toArray());
return result;
}
private JFreeChart createChart2(XYDataset dataset, String title)
{
JFreeChart chart = ChartFactory.createXYLineChart(title, // chart title
"Words",
"Activation",
dataset, // data
PlotOrientation.HORIZONTAL,
true, // include legend
true,
false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setForegroundAlpha(0.5f);
String[] grade = new String[5];
grade[0] = "Temp 0";
grade[1] = "Temp 1";
grade[2] = "Temp 2";
grade[3] = "Temp 3";
grade[4] = "Temp 4";
SymbolAxis rangeAxis = new SymbolAxis("Words", grade);
rangeAxis.setTickUnit(new NumberTickUnit(1));
rangeAxis.setRange(0,grade.length);
plot.setRangeAxis(rangeAxis);
return chart;
}
Using:
plot.setDomainAxis(rangeAxis);
solves my problem.
Thanks to trashgod for the help.

Categories

Resources