How to manipulate a legend in an Incanter chart

I am trying to include a legend in an Incanter diagram, but I am having problems with what I want:

  • I want to be able to instantiate a chart without first data (using [] [] as my xy arguments) and then add data points in a separate step. However, the only way to add a legend is to specify :legend true after the initial x y-points are specified in the constructor. Can't specify :legend true without yy arguments, and I did not find any add-legend function.

  • The legend function captures the code that I use when adding chart data, which means that if I do not want the ugly code to appear in the legend, I need to create nice vars for the X and Y points, and not just call the function in a line.

  • Therefore, the created legend includes [][] used to create an empty section, includes function calls used to obtain data for points, and includes an anonymous function with names (fn*[p1__3813#](second p1__3813#)) which is not communicative for consumers of my chart.

  • I just want to associate a string with each group of points in a legend, like in matlab, excel, etc.

Here is my current code;

 (def lux-ratios-plot (doto (scatter-plot [] [] :legend true :title "Lux/CH0 vs. CH1/CH0" :x-label "CH1/CH0" :y-label "Lux/CH0") (view))) (doseq [dut [incs hals cfls leds]] (add-points lux-ratios-plot (get-vals :CH1/CH0 dut) (get-vals :Lux/CH0 dut) :points true)) ; Show the trend line for each bulb (doseq [fit [inc-fit hal-fit cfl-fit led-fit]] (add-lines lux-ratios-plot (map #(second %) (:x fit)) (:fitted fit))) 

Therefore, is there any way on the Incanter graphs to indicate a legend line with each call (add-lines ...) or (add-points ...) ?

thanks a lot

Michael

+4
source share
2 answers

Each Incanter chart is also a JFreeChart object. Thus, you can use any of the JFreeChart methods to manipulate the Incanter chart.

For example, to remove a legend that you can make (.removeLegend lux-ratio-plot). There is also the addLegend method. I have not tried it myself. Hope this helps.

+2
source

To associate beautiful names with a series of dots or lines, use the keyword :series-label in a command that adds this data to the chart. For instance:

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

This does not apply to another problem in your question: to create a bare scatter plot that has a legend, you must pass empty data to the scatter-plot , i.e. with x and y as empty sequences, since Incanter does not allow you to specify :legend without transferring data to scatter-plot . If x and y are empty (for example, they are nil ), empty data is also displayed as an element in the legend. I do not believe that this problem can be overcome right now (version 1.5.7).

One solution is to transfer real data in a scatter call, along with the :series-label option. However, this can make it difficult to programmatically create a scatterplot when the number of datasets is not known in advance, since you must process the first dataset differently than others.

Another kludgey solution is the following:

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

Using an empty string as the value :series-label means that the label in the legend for the first, empty dataset will not be displayed. Calling set-stroke-color makes the color of an empty dataset (i.e., Dataset 0) transparent. Otherwise, you will have a red dot for this data set in your legend. You will have a small empty space in the legend where the red dot and the empty line belong, but this is less confusing than seeing the red dot there.

0
source

All Articles