在 JFreeChart 中更改温度计中的水银颜色

发布于 2024-12-07 08:22:37 字数 733 浏览 0 评论 0原文

我已经使用 JFreeChart 创建了温度计图表,但我想更改温度计中的水银颜色。我该怎么做?这是到目前为止我的代码;请解释在哪里更改此代码:

final DefaultValueDataset dataset = new DefaultValueDataset(new Double(10));

// create the chart...
final ThermometerPlot plot = new ThermometerPlot(dataset);

final JFreeChart chart = new JFreeChart("Thermometer Demo 2",  // chart title
                                        JFreeChart.DEFAULT_TITLE_FONT,
                                        plot,                  // plot
                                        false);                // include legend

chart.setBackgroundPaint(new Color(241,250,224));
plot.setMercuryPaint();
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setUpperBound(50);

I have created a thermometer chart with JFreeChart, but I want to change the mercury color in thermometer. How can I do this? Here is my code so far; please explain where to change this code:

final DefaultValueDataset dataset = new DefaultValueDataset(new Double(10));

// create the chart...
final ThermometerPlot plot = new ThermometerPlot(dataset);

final JFreeChart chart = new JFreeChart("Thermometer Demo 2",  // chart title
                                        JFreeChart.DEFAULT_TITLE_FONT,
                                        plot,                  // plot
                                        false);                // include legend

chart.setBackgroundPaint(new Color(241,250,224));
plot.setMercuryPaint();
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setUpperBound(50);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2024-12-14 08:22:37

您可能正在寻找 setSubrangePaint(),如下所示。

ThermometerDemo

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;

/** @see http://stackoverflow.com/questions/7597015 */
public class ThermometerDemo extends JPanel {

    private static final int W = 200;
    private static final int H = 2 * W;

    public ThermometerDemo(double value) {
        this.setLayout(new GridLayout());
        DefaultValueDataset dataset = new DefaultValueDataset(value);
        ThermometerPlot plot = new ThermometerPlot(dataset);
        plot.setSubrangePaint(0, Color.green.darker());
        plot.setSubrangePaint(1, Color.orange);
        plot.setSubrangePaint(2, Color.red.darker());
        JFreeChart chart = new JFreeChart("Demo",
            JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        this.add(new ChartPanel(chart, W, H, W, H, W, H,
            false, true, true, true, true, true));
    }

    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Demo");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new GridLayout(1, 0));
                f.add(new ThermometerDemo(40));
                f.add(new ThermometerDemo(60));
                f.add(new ThermometerDemo(80));
                f.pack();
                f.setVisible(true);
            }
        });

    }
}

You may be looking for setSubrangePaint(), demonstrated below.

ThermometerDemo

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;

/** @see http://stackoverflow.com/questions/7597015 */
public class ThermometerDemo extends JPanel {

    private static final int W = 200;
    private static final int H = 2 * W;

    public ThermometerDemo(double value) {
        this.setLayout(new GridLayout());
        DefaultValueDataset dataset = new DefaultValueDataset(value);
        ThermometerPlot plot = new ThermometerPlot(dataset);
        plot.setSubrangePaint(0, Color.green.darker());
        plot.setSubrangePaint(1, Color.orange);
        plot.setSubrangePaint(2, Color.red.darker());
        JFreeChart chart = new JFreeChart("Demo",
            JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        this.add(new ChartPanel(chart, W, H, W, H, W, H,
            false, true, true, true, true, true));
    }

    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Demo");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setLayout(new GridLayout(1, 0));
                f.add(new ThermometerDemo(40));
                f.add(new ThermometerDemo(60));
                f.add(new ThermometerDemo(80));
                f.pack();
                f.setVisible(true);
            }
        });

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文