0

i am doing an assignment which require me to plot a barchart in scalafx. The snippets below are the current code that i am writing. I have been thinking the error for 6 hours now.

 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

I got this error when i try to plot a graph to display data.

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

How do i successfully plot a barchart in this situation? I am still a bit confused on this part. Your expertise and guidance is highly valuable. Thank you

Brian Ruchiadi
  • 331
  • 1
  • 13
  • 29

1 Answers1

0

The error message is clear that you are defining barChart as [String, Int] but yAxis is a Number so

Changing code line

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

to

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

should solve the error you are having.

Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
  • Cannot.. Because if i change it to number the series1, series2, ..., seriesn would nned to be changed to number as well. Meanwhile, it is Int, so, if i change it to number, is there any known way to convert int to number or number to int in scala? – Brian Ruchiadi Jun 15 '17 at 02:39
  • Can you update your question with questionDetails as well so that you get quicker solutions from others including me. – Ramesh Maharjan Jun 15 '17 at 12:40
  • i will update to you ASAP.. Still outside.. Please wait and assist me.. Hahaha. Thank you Ramesh – Brian Ruchiadi Jun 15 '17 at 18:25