我是初学程序员。这是我的学校项目。很多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);
}
}
我究竟如何更改绘制线条的样式和颜色。现在,有一些默认的线条着色和样式。有没有办法从默认值改变它。
答案 0 :(得分:0)
我究竟如何改变绘制线条的样式和颜色。
您可以通过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,了解更多方法来自定义每个系列的呈现方式。