[JFreeChart] - Use the Thermometer Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
phuongdv
Posts: 1
Joined: Tue Jul 10, 2018 8:15 am
antibot: No, of course not.

[JFreeChart] - Use the Thermometer Chart

Post by phuongdv » Tue Jul 31, 2018 3:44 am

JFreeChart is a free graphical javascript library that makes it easy for developers to create a variety of charts from pie charts, bar charts, line charts, 3D charts. This tutorial will show you how to use the Thermometer Chart in JFreeChart.

To learn how to use the Thermometer Chart in JFreeChart we will look at the chart below and then redraw this chart using the Thermometer Chart in JFreeChart.

First, you create the createChart () method that returns the JFreeChart object


private static JFreeChart createChart (ThermometerPlot plot) {
     JFreeChart chart = new JFreeChart ("TEMPERATURE MEASUREMENT",
          JFreeChart.DEFAULT_TITLE_FONT, plot, false);
     plot.setThermometerStroke (new BasicStroke (2.0f));
     plot.setThermometerPaint (Color.lightGray);
     return chart;
}

private static JFreeChart createChart (ThermometerPlot plot) {
     JFreeChart chart = new JFreeChart ("TEMPERATURE MEASUREMENT",
          JFreeChart.DEFAULT_TITLE_FONT, plot, false);
     plot.setThermometerStroke (new BasicStroke (2.0f));
     plot.setThermometerPaint (Color.lightGray);
     return chart;
}
Next, create the createDataset () method that returns the ThermometerPlot object and you pass the measured value.
So I just showed you how to use the Thermometer Chart in JFreeChart, see you in the next articles in the JFreeChart tutorial series.

private static ThermometerPlot createDataset (double value) {
     DefaultValueDataset dataset = new DefaultValueDataset (value);
     ThermometerPlot plot = new ThermometerPlot (dataset);
     return plot;
}

private static ThermometerPlot createDataset (double value) {
     DefaultValueDataset dataset = new DefaultValueDataset (value);
     ThermometerPlot plot = new ThermometerPlot (dataset);
     return plot;
}
Finally, you run the main function using the Thermometer Chart in JFreeChart:


public static void main (String [] args) {
     JFreeChart pieChart = createChart (createDataset (37));
     ChartPanel chartPanel = new ChartPanel (pieChart);
     JFrame frame = new JFrame ();
     frame.add (chartPanel);
     frame.setSize (600, 400);
     frame.setLocationRelativeTo (null);
     frame.setResizable (false);
     frame.setVisible (true);
}
public static void main (String [] args) {
     JFreeChart pieChart = createChart (createDataset (37));
     ChartPanel chartPanel = new ChartPanel (pieChart);
     JFrame frame = new JFrame ();
     frame.add (chartPanel);
     frame.setSize (600, 400);
     frame.setLocationRelativeTo (null);
     frame.setResizable (false);
     frame.setVisible (true);
}
The entire source code uses the Thermometer Chart in JFreeChart as follows:


package thermometer;

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

/ **
 *
 * @horhor TUNGDUONG
 * /
public class ThermometerChartMain {

    private static JFreeChart createChart (ThermometerPlot plot) {
        JFreeChart chart = new JFreeChart ("TEMPERATURE MEASUREMENT",
                JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        plot.setThermometerStroke (new BasicStroke (2.0f));
        plot.setThermometerPaint (Color.lightGray);
        return chart;
    }

    private static ThermometerPlot createDataset (double value) {
        DefaultValueDataset dataset = new DefaultValueDataset (value);
        ThermometerPlot plot = new ThermometerPlot (dataset);
        return plot;
    }

    public static void main (String [] args) {
        JFreeChart pieChart = createChart (createDataset (37));
        ChartPanel chartPanel = new ChartPanel (pieChart);
        JFrame frame = new JFrame ();
        frame.add (chartPanel);
        frame.setSize (600, 400);
        frame.setLocationRelativeTo (null);
        frame.setResizable (false);
        frame.setVisible (true);
    }

}

package thermometer;
 
import java.awt.BasicStroke;
import java.awt.Color;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
 
/ **
 *
 * @horhor TUNGDUONG
 * /
public class ThermometerChartMain {
 
    private static JFreeChart createChart (ThermometerPlot plot) {
        JFreeChart chart = new JFreeChart ("TEMPERATURE MEASUREMENT",
                JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        plot.setThermometerStroke (new BasicStroke (2.0f));
        plot.setThermometerPaint (Color.lightGray);
        return chart;
    }
 
    private static ThermometerPlot createDataset (double value) {
        DefaultValueDataset dataset = new DefaultValueDataset (value);
        ThermometerPlot plot = new ThermometerPlot (dataset);
        return plot;
    }
 
    public static void main (String [] args) {
        JFreeChart pieChart = createChart (createDataset (37));
        ChartPanel chartPanel = new ChartPanel (pieChart);
        JFrame frame = new JFrame ();
        frame.add (chartPanel);
        frame.setSize (600, 400);
        frame.setLocationRelativeTo (null);
        frame.setResizable (false);
        frame.setVisible (true);
    }
 
}
Results after running the chapter

Post Reply