Show 2D chart using XChart with output to PDF and SVG
Code: "SimpleXChart.py". Programming language: Python
DMelt Version 3. Last modified: 04/03/1977. License: Pro
https://datamelt.org/code/cache/SimpleXChart_7861.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
from org.knowm.xchart import BitmapEncoder;
from org.knowm.xchart.BitmapEncoder import BitmapFormat;
from org.knowm.xchart import VectorGraphicsEncoder;
from org.knowm.xchart.VectorGraphicsEncoder import VectorGraphicsFormat;
from org.knowm.xchart import XYChart;
from org.knowm.xchart import XYSeries;
from org.knowm.xchart.style.markers import SeriesMarkers;
# Creates a simple Chart and saves it as a PNG and JPEG image file. */
xData = [0.0, 1.0, 2.0, 3.0, 4.0]
yData = [2.0, 1.0, 0.0, -2.0, -3.0]
# Create Chart
chart = XYChart(500, 400);
chart.setTitle("Sample Chart");
chart.setXAxisTitle("X");
chart.setXAxisTitle("Y");
series = chart.addSeries("y(x)", xData, yData);
series.setMarker(SeriesMarkers.CIRCLE);
# saving options
# BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG);
# BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.JPG);
# BitmapEncoder.saveJPGWithQuality(chart, "./Sample_Chart_With_Quality.jpg", 0.95f);
# BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.BMP);
# BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.GIF);
# BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.PNG, 300);
# BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.JPG, 300);
# BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.GIF, 300);
#VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.EPS);
VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.PDF);
#VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.SVG);
print("Image saved: Sample_Chart.pdf")
You see the box below because you did not login.