jfreechart setbackgroundpaint什么都不做 [英] jfreechart setbackgroundpaint not doing anything

查看:76
本文介绍了jfreechart setbackgroundpaint什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有使用jFreeChart for Swing应用程序的经验的人:

To anyone who has experience with jFreeChart for Swing applications:

我无法更改图表的背景.我的最终目标是为图表设置平滑的渐变背景,但是我根本无法更改任何背景.这行代码chart.setBackgroundPaint(Color.BLUE);应该做某事,对吗?但这并没有改变任何东西.我所得到的是相同的灰色绘图背景和白色图表背景.

I am having trouble changing the background for charts. My ultimate goal is to set a smooth gradient background for my charts, but I am unable to change any backgrounds at all. This line of code chart.setBackgroundPaint(Color.BLUE); should do something, am I right? But it doesn't change anything. All I get is the same grey plot background and white chart background.

我为无法更改颜色而scratch之以鼻……非常感谢您的帮助!

I'm scratching my head as to why I can't get the colors to change... Any help is greatly appreciated!

这是我的代码(如果您有jfreechart库,则可以编译并运行)

Here is my code (compilable & runnable if you have jfreechart library)

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GradientPaint;
    import java.awt.Paint;
    import java.awt.PaintContext;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import static java.awt.Transparency.OPAQUE;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.ColorModel;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;

    /**
     *
     * @author Ryan
     */
    public class SalesGraph extends JPanel {

        /**
         * Creates a sample dataset.
         *
         * @return The dataset.
         */
        private static CategoryDataset createDataset() {
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
            dataset.addValue(212, "Sales", "Jan");
            dataset.addValue(504, "Sales", "Feb");
            dataset.addValue(1520, "Sales", "Mar");
            dataset.addValue(1842, "Sales", "Apr");
            dataset.addValue(2991, "Sales", "May");
            dataset.addValue(3500, "Sales", "June");
            return dataset;
        }

        /**
         * Creates a sample chart.
         *
         * @param dataset  a dataset.
         *
         * @return The chart.
         */
        private static JFreeChart createChart(CategoryDataset dataset, String subTitle) {

            // create the chart...
            JFreeChart chart = ChartFactory.createLineChart(
                "# of Sales by Month",   // chart title
                "Month",                       // domain axis label
                "# of Sales",                   // range axis label
                dataset,                         // data
                PlotOrientation.VERTICAL,        // orientation
                true,                           // include legend
                true,                            // tooltips
                false                            // urls
            );

            if(subTitle != null && !subTitle.isEmpty())
                chart.addSubtitle(new TextTitle(subTitle));
            chart.setBackgroundPaint(Color.BLUE);
    //        Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
    //        chart.setBackgroundPaint(p);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setRangePannable(true);
            plot.setRangeGridlinesVisible(true);
            plot.setBackgroundAlpha(1);
            plot.setBackgroundPaint(Color.BLUE);
    //        Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
    //        plot.setBackgroundPaint(p);


            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

            ChartUtilities.applyCurrentTheme(chart);

            // customise the renderer...
            LineAndShapeRenderer renderer
                    = (LineAndShapeRenderer) plot.getRenderer();
            renderer.setBaseShapesVisible(true);
            renderer.setDrawOutlines(true);
    //        renderer.setUseFillPaint(true);
    //        renderer.setBaseFillPaint(Color.white);
            renderer.setSeriesStroke(0, new BasicStroke(3.0f));
            renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
            renderer.setSeriesShape(0, new Ellipse2D.Double(0,0,0,0));
            renderer.setPaint(Color.RED);
            return chart;
        }

        /**
         * Creates a panel for the demo (used by SuperDemo.java).
         *
         * @return A panel.
         */
        public static JPanel createPanel(CategoryDataset dataset, Dimension size) {
            JFreeChart chart = createChart(dataset, null);
            ChartPanel panel = new ChartPanel(chart);
            panel.setMouseWheelEnabled(true);
            if(size != null)
                panel.setPreferredSize(size);
            return panel;
        }

        public static JPanel createPanel() {
            return createPanel(createDataset(), null);
        }

        /**
         * Starting point for the demonstration application.
         *
         * @param args  ignored.
         */
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            JPanel panel = createPanel();
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
        }
    }

推荐答案

您要在设置背景色之后应用JFreeChart主题 ,因此该颜色在最终图表上不可见.要查看背景颜色为蓝色,只需删除该行

You are applying a JFreeChart theme after setting the background color hence the color is not visible on the final chart. To see the background color as blue simply remove the line

ChartUtilities.applyCurrentTheme(chart);

这篇关于jfreechart setbackgroundpaint什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆