Source code

Download source archive ↓

upstream/piflow-bundle/src/main/scala/cn/piflow/bundle/generic/visualization/ScatterPlotChart.scala

1package cn.piflow.bundle.generic.visualization
2
3import cn.piflow.{JobContext, JobInputStream, JobOutputStream, ProcessContext}
4import cn.piflow.conf.{ConfigurableVisualizationStop, Port, StopGroup, VisualizationType}
5import cn.piflow.conf.bean.PropertyDescriptor
6import cn.piflow.conf.util.{ImageUtil, MapUtil}
7import org.apache.spark.sql.SparkSession
8
9class ScatterPlotChart extends ConfigurableVisualizationStop{
10  override val authorEmail: String = "xjzhu@cnic.cn"
11  override val description: String = "Show data with scatter plot chart." +
12    "Dimension is represented by customizedProperties, " +
13    "the key of customizedProperty is the dimentsion column, and the value is the index of the dimension. " +
14    "The first index represent abscissa, the second index represent ordinate. "
15  override val inportList: List[String] = List(Port.DefaultPort)
16  override val outportList: List[String] = List(Port.DefaultPort)
17
18  var legend:String =_
19
20  override var visualizationType: String = VisualizationType.ScatterPlot
21  override val isCustomized: Boolean = true
22  //override val customizedAllowValue: List[String] = List("COUNT","SUM","AVG","MAX","MIN")
23  override def setProperties(map: Map[String, Any]): Unit = {
24    legend=MapUtil.get(map,key="legend").asInstanceOf[String]
25  }
26
27  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
28    var descriptor : List[PropertyDescriptor] = List()
29    val abscissa = new PropertyDescriptor()
30      .name("legend")
31      .displayName("Legend")
32      .description("The legend  of bubble chart")
33      .defaultValue("")
34      .example("year")
35      .required(true)
36
37    descriptor = abscissa :: descriptor
38    descriptor
39  }
40
41  override def getIcon(): Array[Byte] = {
42    ImageUtil.getImage("icon/visualization/scatter-plot.png")
43  }
44
45  override def getGroup(): List[String] = {
46    List(StopGroup.Visualization)
47  }
48
49  override def initialize(ctx: ProcessContext): Unit = {}
50
51  override def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
52    val spark = pec.get[SparkSession]()
53    val sqlContext=spark.sqlContext
54    val dataFrame = in.read()
55    dataFrame.createOrReplaceTempView("ScatterPlot")
56
57    if(this.customizedProperties != null || this.customizedProperties.size != 0){
58
59      println("dimension is " + this.customizedProperties.keySet.mkString(",") + "!!!!!!!!!!!!!!!")
60
61      var dimensionArray = List(this.customizedProperties.toSeq.sortBy(_._2): _*).map( x => x._1)
62      println("ordered dimension is " + dimensionArray.mkString(",") + "!!!!!!!!!!!!!!!")
63
64      val sqlText = "select " + legend + "," + dimensionArray.mkString(",") + " from ScatterPlot order by " + legend + "," + dimensionArray(0);
65      println("ScatterPlot Sql: " + sqlText)
66      val scatterPlottDF = spark.sql(sqlText)
67      out.write(scatterPlottDF.repartition(1))
68    }else{
69      out.write(dataFrame)
70    }
71  }
72}