如何使用 Incanter 获取归一化的质押条形图?

How to get a normalized staked bar chart using Incanter?

提问人:leontalbot 提问时间:12/3/2016 最后编辑:leontalbot 更新时间:12/3/2016 访问量:282

问:

在此示例中,

(use '(incanter core stats charts datasets))

(with-data  (get-dataset :airline-passengers)
  (view (stacked-bar-chart :month :passengers :group-by :year :legend true)))

如何使每个全局柱等于 100%,从而允许堆叠元素之间的完全相对比较?

现在我得到这样的东西:http://phloxblog.in/angulard3/images/Nor-Stackbar.jpg

我怎样才能做到这一点?enter image description here

Clojure jfreechart incanter

评论


答:

1赞 leontalbot 12/3/2016 #1

我找到了答案。

(use '(incanter core stats charts datasets))

(import
   '[org.jfree.chart ChartFactory ChartUtilities JFreeChart StandardChartTheme]
   '[org.jfree.chart.plot CategoryPlot]
   '[org.jfree.chart.renderer.category CategoryItemRenderer StackedBarRenderer])

(with-data  (get-dataset :airline-passengers)
  (view 
   (let [chart (stacked-bar-chart :month :passengers :group-by :year :legend true)
         plot (-> chart .getCategoryPlot)
         renderer (-> plot .getRenderer)
         _ (.setRenderAsPercentages renderer true)] 
     chart)))

以前:enter image description here

后:enter image description here

如果你想得到以百分比为单位的轴,你可以做到

(use '(incanter core stats charts datasets))

(import
   '[org.jfree.chart ChartFactory ChartUtilities JFreeChart StandardChartTheme]
   '[org.jfree.chart.axis NumberAxis]
   '[org.jfree.chart.labels StandardCategoryItemLabelGenerator ItemLabelPosition ItemLabelAnchor]
   '[org.jfree.chart.plot CategoryPlot]
   '[org.jfree.chart.renderer.category StackedBarRenderer]
   '[java.text DecimalFormat])

(with-data (get-dataset :airline-passengers)
  (view 
   (let [chart (stacked-bar-chart :month :passengers :group-by :year :legend true)
         plot (-> chart .getCategoryPlot)
         range-axis (-> plot .getRangeAxis)
         percent-format (DecimalFormat. "#%") ;;"#.0%"
         _ (.setNumberFormatOverride range-axis percent-format)
         renderer (-> plot .getRenderer) 
         _ (.setRenderAsPercentages renderer true)] 
     chart)))