Jayson Jayson - 1 month ago 7
Scala Question

addValue to Dataset in Scala and jfreechart

There is a ListMap with the below values:

scala> mx_sorted

res30: scala.collection.immutable.ListMap[Int,Int] = Map(0 -> 0, 5 -> 1, 10 -> 10, 15 -> 66, 20 -> 157, 25 -> 175, 30 -> 135, 35 -> 106, 40 -> 88, 45 -> 80, 50 -> 62, 55 -> 32, 60 -> 19, 65 -> 8, 70 -> 4, 75 -> 0, 80 -> 0)


scala> mx_sorted.foreach(println)

(0,0)
(5,1)
(10,10)
(15,66)
(20,157)
(25,175)
(30,135)
(35,106)
(40,88)
(45,80)
(50,62)
(55,32)
(60,19)
(65,8)
(70,4)
(75,0)
(80,0)


Created a DefaultCategoryDataset

val ds = new org.jfree.data.category.DefaultCategoryDataset


When I am trying to addvalues to this dataset, getting error.

scala> mx_sorted.foreach{case(k,v) => ds.addValue(v,"UserAges",k)}

<console>:41: error: overloaded method value addValue with alternatives:
(x$1: Double,x$2: Comparable[_],x$3: Comparable[_])Unit <and>
(x$1: Number,x$2: Comparable[_],x$3: Comparable[_])Unit
cannot be applied to (Int, String, Int)
mx_sorted.foreach{case(k,v) => ds.addValue(v,"UserAges",k)}


Kindly help in rectifying this.

Answer Source

The fast fix is {case(k,v) => ds.addValue(v: Double, "UserAges", k: java.lang.Integer)} (java.lang. isn't necessary and probably neither is : Double, I am just including it to make it more clear). The issue is that Int doesn't extend Comparable (because it corresponds to the primitive JVM int type), so it needs to be boxed to java.lang.Integer which does.