gpt4 book ai didi

java - 如何使图表显示数组元素

转载 作者:行者123 更新时间:2023-12-02 06:33:45 25 4
gpt4 key购买 nike

我有 6 个数字,都是从 0 到 100。我需要将其放入图表中,我已经有了图表的代码,但不知道如何将其链接到我的数组。该图表使用条形图。这是我的代码,它是我拥有的其他两个数组的平均值:

public static void computeResults(double[] examMarks, double[] courseworkmarks)
{
double avgMarks[] =new double[examMarks.length];
System.out.println ("The total average of each module is ");
for(int i=0;i<avgMarks.length;i++){
int cwWeighting=40;
avgMarks[i]=(examMarks[i]*(100-cwWeighting)+courseworkmarks[i]*cwWeighting)/100;
System.out.print(avgMarks[i] + "\t" );
}
}

最佳答案

以下是如何在 JFreechart 中设置条形图值的基本示例,它可能会对您有所帮助,具体取决于您用于创建图表的库:

public class BarChartExample {
public static void main(String[] args) {
// Create a simple Bar chart

double[] dub = {12.2, 15.4, 18.3, 9.3, 7.7}; //Array
String[] student = {"Bob", "Dave", "William", "Boris", "Rick"}; //Array

DefaultCategoryDataset dataset = new DefaultCategoryDataset(); //Create dataset
for(int i = 0; i < dub.length; i++){
dataset.setValue(dub[i], "Marks", student[i]); //Setting the values
}

JFreeChart chart = ChartFactory.createBarChart3D("Goal comparison",
"Marks", "Students", dataset, PlotOrientation.VERTICAL,
false, true, false); //Chart creation
try {
ChartUtilities.saveChartAsJPEG(new File("D:\\Users\\user2777005\\Desktop\\Barchart.jpg"), chart, 500, 300);
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}}}

祝你好运!

关于java - 如何使图表显示数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19854625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com