当前位置: 首页 > 工具软件 > jCharts > 使用案例 >

jcharts画线图,饼图和柱状图

经兴安
2023-12-01
正在做的一个分析软件中用到了图表展现,网上搜了一下,决定使用jchats,下面是一个简单的jcharts使用的例子(需要jar包 jCharts-0.7.5.jar):chart.jsp

<%@ page import="java.awt.*,org.jCharts.*,org.jCharts.chartData.*,org.jCharts.properties.*,org.jCharts.types.ChartType,org.jCharts.axisChart.*,org.jCharts.test.TestDataGenerator,org.jCharts.encoders.JPEGEncoder13,org.jCharts.properties.util.ChartFont,
                      org.jCharts.encoders.ServletEncoderHelper,org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer,org.jCharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition,
                      org.jCharts.nonAxisChart.PieChart2D"%><%
    
    try
    {
        String picType = "bar";
        
        LegendProperties legendProperties = new LegendProperties();
        ChartProperties chartProperties = new ChartProperties();
        
        if("pie".equals(picType)){
            //**--饼图--**/
            
            //饼图属性
            PieChart2DProperties pieChart2Dproperties = new PieChart2DProperties(); 
            
            double[] data2 = new double[]{40, 15, 35, 65, 59};
            Paint[] paints2 = new Paint[]{Color.blue, Color.red, Color.green, Color.yellow, Color.white};
            String[] labels = {"BMW", "Honda", "Lexus", "Audi", "Acura"};
            PieChartDataSet pieChartData = new PieChartDataSet( "Cars That Own!", data2, labels, paints2, pieChart2Dproperties );
            PieChart2D pieChart2D = new PieChart2D( pieChartData, legendProperties, chartProperties, 550, 360);
            ServletEncoderHelper.encodeJPEG13( pieChart2D, 1.0f, response );
            
            //**--饼图--**/
        }else{
            //**--线图,柱状图--**//        
            
              String[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004"};
                String xAxisTitle= "Years";
                String yAxisTitle= "Problems";
                String title= "Micro$oft At Work";
                
                double[][] data= TestDataGenerator.getRandomNumbers( 3, 7, 200, 500 );
                String[] legendLabels= { "Bugs", "Security Holes", "Backdoors" };
                
                AxisProperties axisProperties = new AxisProperties( false );
                //线图属性
                Stroke[] strokes= { LineChartProperties.DEFAULT_LINE_STROKE, LineChartProperties.DEFAULT_LINE_STROKE, LineChartProperties.DEFAULT_LINE_STROKE };
                Shape[] shapes= { PointChartProperties.SHAPE_TRIANGLE,PointChartProperties.SHAPE_DIAMOND, PointChartProperties.SHAPE_CIRCLE };
                LineChartProperties lineChartProperties = new LineChartProperties(strokes,shapes);
                
                //柱状图属性
                BarChartProperties barChartProperties = new BarChartProperties();
                ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer( false, false, true, -1 );
                valueLabelRenderer.setValueLabelPosition( ValueLabelPosition.ON_TOP );
                valueLabelRenderer.useVerticalLabels( false );
                barChartProperties.addPostRenderEventListener( valueLabelRenderer );
                    
                Paint[] paints= TestDataGenerator.getRandomPaints( 3 );

                ChartFont axisScaleFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.black );
                axisProperties.getXAxisProperties().setScaleChartFont( axisScaleFont );
                axisProperties.getYAxisProperties().setScaleChartFont( axisScaleFont );

                ChartFont axisTitleFont = new ChartFont( new Font( "Arial Narrow", Font.PLAIN, 14 ), Color.black );
                axisProperties.getXAxisProperties().setTitleChartFont( axisTitleFont );
                axisProperties.getYAxisProperties().setTitleChartFont( axisTitleFont );

                DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle,title );
                
                //From AxisChartServlet.java:createAxisChartDataSet
                AxisChartDataSet acds = null;
                
                if("line".equals(picType))
                    acds = new AxisChartDataSet(data, legendLabels, paints,ChartType.LINE, lineChartProperties );
                else if("bar".equals(picType))
                    acds = new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties );
                
                dataSeries.addIAxisPlotDataSet(acds);
                AxisChart axisChart = new AxisChart(dataSeries, chartProperties, axisProperties,legendProperties, 550, 360);
                ServletEncoderHelper.encodeJPEG13(axisChart, 1.0f, response);
                
            //**--线图,柱状图--**//        
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
    
%>

 类似资料: