簡體   English   中英

使用jfreechart,我將如何更改系列的顏色和樣式

[英]Using jfreechart, how would I change the color and style of the series

我是一個初學者程序員。 這是我上學的項目。 許多jfreechart實現代碼不是我的特定代碼,但是它是其他教程所采用的。

import java.awt.BasicStroke;
import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import java.util.ArrayList;

/**
 * Resource class 
*/
public class Interest {
/**
 * Instance variables
 */
private double interestRate; //The rate of interest earned
private int time; //How many of one unit of time measurement -- generally, this is in terms of years
private int rate; //How many times during one unit of time will the interest compound 
private double investment; //The amount of money that's being invested
private final double inflation = .025; //The rate of which nominal value of increases by


/**
 * Default constructor
 * Default values
 */
public Interest() 
{
    time = 6;
    rate = 1;
    investment = 2000.00;
    interestRate = 2;
}

/**
 * 4-argument constructor
 * @param i
 * @param ir
 * @param t
 * @param r
 */
public Interest(double i, double ir, int t, int r)
{
    investment = i;
    time = t;
    rate =r;
    interestRate = ir;
}

/**
 * Class methods
 * @param time
 */

public void setTime(int time) //Sets the time instance variable in the resource class
{
    this.time = time;
}

public void setRate(int rate) //Sets the rate instance variable in the resource class
{
    this.rate = rate;
}

public void setInterestRate(double interestRate) //Sets the interest rate instance variable in the resource class
{
    this.interestRate = interestRate;
}

public void setInvestment(double investment) //Sets the investment instance variable in the resource class
{
    this.investment = investment;
}

public int getTime() //Gets the time instance variable in the resource class
{
    return time;
}

public double getInvestment() //Gets the investment instance variable in the resource class
{
    return investment;  
}

public int getRate() //Gets the rate instance variable in the resource class
{
    return rate;
}

public double getInterestRate() //Gets the interest rate instance variable in the resource class
{
    return interestRate;
}

public double cumulativeInvestmentValue() //Calculates the value of your investment over a certain period of time
{
    double decimalInterest = (double)(interestRate/100.00);
    return investment * (Math.pow(((decimalInterest/rate) + 1), (time * rate)));

}
public String getEquation() //Returns the equation of the function
{
    return "" + investment + "(1 + " + interestRate/100 + "/" + rate + ")^(x * " + rate + ")";
}

public double getDerivative(double x) //Calculates the slope of the tangent line at any given point
{
    int t = time;
    double r = rate;
    double i = investment;
    double ir = interestRate;
    double decimalInterestRate = ir/100;

    return  i * (r * (Math.pow((1 + decimalInterestRate/r), x * r)) * ((Math.log(1 + (decimalInterestRate/r))))); //This is the formula to find the derivative of any exponential function
}


public void cumulativeProgressiveInterestValue() //Prints a table of your investment, showing the value of it over time
{

    int iteration = 0;
    double realSum = investment;
    int t = time;
    int r = rate;
    double i = investment;
    double ir = interestRate;
    double decimalInterestRate = ir/100;

    Interest temp = new Interest(i, ir, t, r);

    ArrayList<Integer> time = new ArrayList<Integer>();
    ArrayList<Double> derivative = new ArrayList<Double>();
    ArrayList<Double> nominalInterestSum = new ArrayList<Double>();
    ArrayList<Double> realInterestSum = new ArrayList<Double>();


    for(int x = 0; x <= t; x++)
    {
        time.add(iteration);
        iteration++;
        derivative.add(temp.getDerivative(x));
        nominalInterestSum.add(i * (Math.pow(((decimalInterestRate/r) + 1), (x * r))));
        realInterestSum.add((((Math.pow(((decimalInterestRate/r) + 1), (x * r)))/((Math.pow(((inflation) + 1), (x))))) * i));
        //realInterestSum.add(i * (Math.pow((((decimalInterestRate - inflation)/r) + 1), (x * r))));
    }

    System.out.println("InterestRate: " + ir + "\nRate: " + r + "\nInvestment: " + i + "\nTime: " + t);
    System.out.format("%-15s%-20s%-28s%-29s\n", "Time", "d/dx(Slope)", "Nominal sum accumulation", " Real sum accumulation");

    for(int x = 0; x <= t; x++)
    {
        System.out.format("%-15d%-20f%-29f%-29f\n", time.get(x), derivative.get(x), nominalInterestSum.get(x), realInterestSum.get(x));
    }
}


private XYDataset createDataset() {

    final XYSeries series1 = new XYSeries("Investment Function");
    final XYSeries series2 = new XYSeries("Real Investment Function");
    final XYSeries series3 = new XYSeries("Derivative Function");

    int iteration = 0;
    double realSum = investment;
    int t = time;
    int r = rate;
    double i = investment;
    double ir = interestRate;
    double decimalInterestRate = ir/100;

    Interest temp = new Interest(i, ir, t, r);

    ArrayList<Integer> time = new ArrayList<Integer>();
    ArrayList<Double> derivative = new ArrayList<Double>();
    ArrayList<Double> nominalInterestSum = new ArrayList<Double>();
    ArrayList<Double> realInterestSum = new ArrayList<Double>();


    for(int x = 0; x <= t; x++)
    {
        time.add(iteration);
        iteration++;
        derivative.add(temp.getDerivative(x));
        nominalInterestSum.add(i * (Math.pow(((decimalInterestRate/r) + 1), (x * r))));
        realInterestSum.add((((Math.pow(((decimalInterestRate/r) + 1), (x * r)))/((Math.pow(((inflation) + 1), (x))))) * i));
        //realInterestSum.add(i * (Math.pow((((decimalInterestRate - inflation)/r) + 1), (x * r))));
    }

    for(int x = 0; x <= t; x++)
    {
        series1.add(time.get(x), nominalInterestSum.get(x));
        series2.add(time.get(x), realInterestSum.get(x));
        series3.add(time.get(x), derivative.get(x));
    }

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series1);
    dataset.addSeries(series2);
    dataset.addSeries(series3);

    return dataset;

}

private JFreeChart createChart(final XYDataset dataset) {

    final JFreeChart chart = ChartFactory.createXYLineChart("Interest Investment", "Time", "Money", dataset, PlotOrientation.VERTICAL, true, true, false);

    chart.setBackgroundPaint(Color.white);

    XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShapesVisible(1, false);
    renderer.setSeriesStroke(0, new BasicStroke(4));
    /*renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, false);
    renderer.setSeriesLinesVisible(1, false);
    renderer.setSeriesShapesVisible(1, true);*/



    plot.setRenderer(renderer);

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

    return chart;

}

public void displayChart(final String title) {

    final XYDataset dataset = createDataset();
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    ChartFrame frame = new ChartFrame(title, chart);
    frame.pack(); 
    frame.setVisible(true);
}


}

我將如何精確改變所繪制線條的樣式和顏色。 現在,有一些默認的線條顏色和樣式。 有什么辦法可以更改默認設置。

我將如何精確改變所繪制線條的樣式和顏色。

您可以通過XYLineAndShapeRenderer (例如,在createChart方法中創建的序列)更改每個系列的渲染方式。 例如,要更改第一個系列(索引0)的呈現方式:

renderer.setSeriesPaint(0,Color.CYAN);//change rendered color to cyan
renderer.setSeriesLinesVisible(0, false);//no lines between points
renderer.setSeriesStroke(0, new BasicStroke(4));//the thickness of any lines being rendered

請參閱所提供鏈接的API,以獲取更多自定義每個系列呈現方式的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM