斯卡拉:我如何成功绘制这个条形图?

时间:2017-06-14 16:57:45

标签: scala bar-chart scalafx

我正在做一项任务,要求我在scalafx中绘制条形图。 下面的片段是我正在编写的当前代码。我一直在想这个错误6个小时。

 var A : Int = 0
 var B : Int = 0
 var C : Int = 0
 var D : Int = 0
 var F : Int = 0

 val xAxis : CategoryAxis = new CategoryAxis()
 val yAxis : NumberAxis = new NumberAxis("Marks", 0.0d, 45.0d, 5.0d)
 val barChart : BarChart[String, Int] = new BarChart[String, Int](xAxis, yAxis)

 barChart.setTitle("Question Summary")
 xAxis.setLabel("Category")
 yAxis.setLabel("Number")

 for(index <- 0 to questionDetails.length - 1){
    if(questionDetails.apply(index).asInstanceOf[Double] < 40.0){
        F += 1
    }
    if(questionDetails.apply(index).asInstanceOf[Double] < 50.0 &&
        questionDetails.apply(index).asInstanceOf[Double] >= 40.0){
        D += 1
    }
    if(questionDetails.apply(index).asInstanceOf[Double] < 60.0 &&
        questionDetails.apply(index).asInstanceOf[Double] >= 50.0){
        C += 1
    }
    if(questionDetails.apply(index).asInstanceOf[Double] < 70.0 &&
        questionDetails.apply(index).asInstanceOf[Double] >= 60.0){
        B += 1
    }
    if(questionDetails.apply(index).asInstanceOf[Double] >= 70.0){
        A += 1
    }
 }

 val series1 : XYChart.Series[String, Int] = new XYChart.Series()
 val series2 : XYChart.Series[String, Int] = new XYChart.Series()
 val series3 : XYChart.Series[String, Int] = new XYChart.Series()
 val series4 : XYChart.Series[String, Int] = new XYChart.Series()
 val series5 : XYChart.Series[String, Int] = new XYChart.Series()

 val data1 : XYChart.Data[String, Int] =  XYChart.Data(A.toString, A)
 val data2 : XYChart.Data[String, Int] =  XYChart.Data(B.toString, B)
 val data3 : XYChart.Data[String, Int] =  XYChart.Data(C.toString, C)
 val data4 : XYChart.Data[String, Int] =  XYChart.Data(D.toString, D)
 val data5 : XYChart.Data[String, Int] =  XYChart.Data(F.toString, F)

 series1.getData().add(data1)
 series2.getData().add(data2)
 series3.getData().add(data3)
 series4.getData().add(data4)
 series5.getData().add(data5)

 barChart.prefWidth = 300.0
 barChart.getData().addAll(series1, series2, series3, series4, series5)

 questionAnalysisPane.left = barChart

当我尝试绘制图表来显示数据时,我收到此错误。

found   : scalafx.scene.chart.NumberAxis
[error]  required: scalafx.scene.chart.Axis[Int]
[error]      val barChart : BarChart[String, Int] = new BarChart[String, Int]
(xAxis, yAxis)

如何在这种情况下成功绘制条形图? 我仍然对这部分感到困惑。 您的专业知识和指导非常有价值。 谢谢

1 个答案:

答案 0 :(得分:0)

错误消息很明显,您将ifstream infile; infile.open("Info.txt"); string x; infile >> x; cout << "Hello" << x << endl; 定义为barChart,但[String, Int]yAxis所以

更改代码行

Number

 val barChart : BarChart[String, Int] = new BarChart[String, Int](xAxis, yAxis)

应解决您遇到的错误。