0

我正在使用 JFreeChart 作为我的 java 应用程序的库,并且我正在尝试设置多个 CRITICAL 子范围。

    // create a dataset...
    final DefaultValueDataset dataset = new DefaultValueDataset(new Double(10));

    // create the chart...
    final ThermometerPlot plot = new ThermometerPlot(dataset);
    final JFreeChart chart = new JFreeChart("Test", JFreeChart.DEFAULT_TITLE_FONT, plot, false);   

    plot.setRange(-20, 100);

    plot.setSubrange(ThermometerPlot.CRITICAL, -20.0, 0.0);
    plot.setSubrange(ThermometerPlot.NORMAL, 0.0, 40.0);
    plot.setSubrange(ThermometerPlot.WARNING, 40.0, 70.0);
    plot.setSubrange(ThermometerPlot.CRITICAL, 70.0, 100.0);

    plot.setThermometerPaint(Color.lightGray);
    final ChartPanel chartPanel = new ChartPanel(chart);
    getContentPane().add(chartPanel);

它不起作用:(我的错误是什么?

提前致谢

4

1 回答 1

0

您好,您可以将默认 MercuryPaint 设置为红色并选择一个临界范围并定义正常和警告,然后删除此行:

    plot.setSubrange(ThermometerPlot.CRITICAL, 70.0, 100.0); 

就是这样。我做了我自己的例子,正常范围为 20-24,并且低于和高于该值。这是我的代码:

package com.tesis.ejemplo;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.ApplicationFrame;

/**
 * A simple demonstration application showing how to create a thermometer.
 */

public class ThermometerDemo2 extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */

    private JTextField txtField = new JTextField();
    private  DefaultValueDataset dataset;

    public ThermometerDemo2(final String title) {

        super(title);

        // create a dataset...
        dataset = new DefaultValueDataset(new Double(24.0));

        // 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

        plot.setThermometerStroke(new BasicStroke(2.0f));
        plot.setThermometerPaint(Color.lightGray);
        plot.setRange(-10.0, 70.0);
        plot.setSubrangeInfo(1, 19.0, 22.0, -10.0, 22.0);
        plot.setSubrangeInfo(0, 22.0, 24.0, 10.0, 26.0);
        plot.setMercuryPaint(Color.red);
        plot.setSubrangePaint(1, Color.green);

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        setContentPane(chartPanel);
        chartPanel.add( txtField  );

        txtField.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                dataset.setValue(  Integer.getInteger( txtField.getText() ) );


            }
        });

    }

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

        final ThermometerDemo2 demo = new ThermometerDemo2("Thermometer Demo 2");
        demo.pack();
        demo.setVisible(true);

    }

}
于 2013-04-25T15:22:58.533 回答