How to manipulate legend in Incanter chart

烂漫一生 提交于 2019-12-05 17:09:25

Every Incanter chart is also a JFreeChart object. So you could use any of the JFreeChart methods to manipulate your Incanter chart.

For example to remove the legend you can do (.removeLegend lux-ratios-plot). There is also an addLegend method. Haven't tried that one myself. Hope this helps.

To associate nice names with series of points, or lines, use the keyword :series-label in the command that adds that data to the chart. For example:

(def c (scatter plot x y :legend true))
(add-lines c x1 y1 :series-label "Primary")
(add-lines c x2 y2 :series-label "Secondary")

This doesn't address the other problem in your question: In order to create a bare scatter plot that has a legend, you have to pass empty data to scatter-plot, i.e. with x and y as empty sequences above, since Incanter doesn't allow you to specify :legend without passing data to scatter-plot. If x and y are empty (e.g. they are nil), the empty data shows up in as an element the legend, too. I don't believe that this problem can be overcome directly currently (version 1.5.7).

One solution is to pass real data in the scatter-plot call, along with a :series-label parameter. However, that may make it more awkward to programmatically generate a scatter plot when the number of datasets is unknown in advance, since you have to treat the first dataset differently than the others.

Another, kludgey solution is this:

(def chart (scatter-plot nil nil :legend true :series-label ""))
(set-stroke-color chart (java.awt.Color. 0 0 0 0) :dataset 0)

Using the empty string as the value of :series-label means that the label in the legend for the first, empty dataset will not display. The set-stroke-color call makes the color of the empty dataset (i.e. dataset 0) transparent. Otherwise, you'll have a red dot for this dataset in your legend. You will have a small empty space in the legend where the red dot and the empty string belong, but that's less confusing than seeing a red dot there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!