Customizing chart colors and styles

In the last recipe, we customized the type of chart and how the data is displayed and laid out. However, we'll often want to customize other things, such as the chart's color scheme or style.

In this recipe, we'll take a chart that we created for another recipe and change the way it looks. This will give an example of how to customize the color and other parts of how the chart displays.

Getting ready

We'll use the same dependencies in our project.clj file as we did in Creating scatter plots with Incanter, and this set of imports in our script or REPL:

(require '[incanter.core :as i]
         '[incanter.charts :as c]
         '[incanter.io :as iio]
         '[incanter.stats :as s])
(import org.jfree.chart.renderer.category.LayeredBarRenderer
        org.jfree.util.SortOrder)

We'll start with the chart we made in Adding lines to scatter charts. We'll keep it assigned to the iris-petal-scatter variable.

How to do it...

For this example, we'll take the Iris petal scatter plot, with the line added, and we'll change the color scheme for St. Patrick's Day (lots of green).

  1. First, we'll pick the colors that we want in the color scheme:
    (def colors [(java.awt.Color. 0x00 0xb3 0x66)
                 (java.awt.Color. 0x80 0xff 0xc9)])
  2. Then we'll set the series color for each renderer on this chart:
    (doseq [[i c] (map vector (range) colors)]
      (.. iris-petal-scatter
          getPlot
          (getRenderer i)
          (setSeriesPaint 0 c)))
  3. Finally, we'll just view it as we viewed the previous charts:
    (i/view iris-petal-scatter)

    This gives us the same chart as before, but with an updated color scheme. The updated chart looks like the following:

    How to do it...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset