2021년 3월 4일 목요일

[Jython] JavaFx Chart Image Creation

JavaFx Chart Image Creation

import sys
from java.io import *
from java.util import *
from javax.imageio import ( ImageIO )
from javafx.application import Application
from javafx.scene import Scene
from javafx.scene.chart import ( LineChart, AreaChart, BarChart, ScatterChart, StackedBarChart, XYChart, NumberAxis, CategoryAxis )
from javafx.scene.chart import PieChart
from javafx.scene.chart.PieChart import Data
from javafx.scene.image import ( WritableImage )
from javafx.collections import FXCollections
from javafx.embed.swing import SwingFXUtils;

#
# Fx Chart
#

class FxVars():
    def __init__(self):
        self.handler = None
        self.outfile = None

__fx_vars = FxVars()

class FxMain(Application):
    def __init__(self): 
        print('__init__()', self)
    def start(self, stage): 
        try:
            image = __fx_vars.handler()
            ImageIO.write( SwingFXUtils.fromFXImage(image, None)
                    , "png"
                    , File(__fx_vars.outfile))
        except Exception as e:
            print(e)
        sys.exit(0);
    def run(self):
        self.launch(self.class, [])
        #return chart.snapshot(SnapshotParameters(), null)
    def set_handler(self,handler):
        __fx_vars.handler = handler
    def set_outfile(self,outfile):
        __fx_vars.outfile = outfile
        
#
# Application
#
            

def getPieData():
    return FXCollections.observableArrayList(
        PieChart.Data("Peachs", 13),
        PieChart.Data("Oranges", 25),
        PieChart.Data("Plums", 10),
        PieChart.Data("Pears", 22),
        PieChart.Data("Apples", 30))

def getPieChart(title, width, height, data):
    chart = PieChart(data)
    scene = Scene(chart, width, height)
    chart.setTitle(title);
    return scene.snapshot(None);

def PieChartHandler():
    return getPieChart("Fruits",640,480,getPieData());

def getXyChartData():
        title  = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
        value1 = [ 23, 14, 15, 24, 34, 36, 22, 45, 43, 17, 29, 25, ]
        value2 = [ 33, 34, 25, 44, 39, 16, 55, 54, 48, 27, 37, 29, ]
        series1 = XYChart.Series()
        series1.setName("Data 1");
        for i in range(len(title)):
            series1.getData().add(XYChart.Data( title[i], value1[i] ))
        series2 = XYChart.Series()
        series2.setName("Data 2")
        for i in range(len(title)):
            series2.getData().add(XYChart.Data( title[i], value2[i] ))
        #lineChartData = ArrayList()
        #lineChartData.add(series1)
        #lineChartData.add(series2)
        return [ series1, series2 ]

def getXyChartDataH():
        title  = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
        value1 = [ 23, 14, 15, 24, 34, 36, 22, 45, 43, 17, 29, 25, ]
        value2 = [ 33, 34, 25, 44, 39, 16, 55, 54, 48, 27, 37, 29, ]
        series1 = XYChart.Series()
        series1.setName("Data 1");
        for i in range(len(title)):
            series1.getData().add(XYChart.Data( value1[i], title[i] ))
        series2 = XYChart.Series()                        
        series2.setName("Data 2")                         
        for i in range(len(title)):                       
            series2.getData().add(XYChart.Data( value2[i], title[i] ))
        return [ series1, series2 ]
        
def getXyChart(title, width, height, data, chart_type="line"):
    xAxis = CategoryAxis()
    yAxis = NumberAxis()
    if   chart_type == "area": chart = AreaChart(xAxis,yAxis)
    elif chart_type == "line": chart = LineChart(xAxis,yAxis)
    elif chart_type == "stackedbar": chart = StackedBarChart(xAxis,yAxis)
    elif chart_type == "bar": chart = BarChart(xAxis,yAxis)
    elif chart_type == "scatter": chart = ScatterChart(xAxis,yAxis)
    elif chart_type == "hbar": 
        chart = BarChart(yAxis,xAxis)
        xAxis.setTickLabelRotation(90)
    else: return None
    xAxis.setLabel("Number of Month")
    xAxis.setAnimated(False)
    xAxis.setTickLabelsVisible(True)
    yAxis.setLabel("Values")
    yAxis.setAnimated(False)
    yAxis.setTickLabelsVisible(True)
    chart.setTitle(title)
    #chart.getData().addAll(data.toArray(XYChart.Series[data.size()]))
    chart.getData().addAll(data)
    scene = Scene(chart, width, height);
    return scene.snapshot(None)
    
def LineChartHandler():        
    return getXyChart("Annunal Trend",640,480,getXyChartData(),chart_type="line")

def AreaChartHandler():        
    return getXyChart("Annunal Trend",640,480,getXyChartData(),chart_type="area")

def BarChartHandler():        
    return getXyChart("Annunal Trend",640,480,getXyChartData(),chart_type="bar")

def HBarChartHandler():        
    return getXyChart("Annunal Trend",640,480,getXyChartDataH(),chart_type="hbar")

def StackedBarChartHandler():        
    return getXyChart("Annunal Trend",640,480,getXyChartData(),chart_type="stackedbar")

def ScatterChartHandler():        
    return getXyChart("Annunal Trend",640,480,getXyChartData(),chart_type="scatter")

if __name__ == "__main__":
    fx = FxMain()
    #fx.set_handler(PieChartHandler); fx.set_outfile(r"D:\pie.png")
    #fx.set_handler(LineChartHandler); fx.set_outfile(r"D:\line.png")
    #fx.set_handler(AreaChartHandler); fx.set_outfile(r"D:\area.png")
    #fx.set_handler(BarChartHandler); fx.set_outfile(r"D:\bar.png")
    #fx.set_handler(HBarChartHandler); fx.set_outfile(r"D:\hbar.png")
    #fx.set_handler(StackedBarChartHandler); fx.set_outfile(r"D:\stackedbar.png")
    fx.set_handler(ScatterChartHandler); fx.set_outfile(r"D:\scatter.png")
    
    fx.run()
    
    

댓글 없음:

댓글 쓰기