Skip to content

Instantly share code, notes, and snippets.

@fxg42
Created December 14, 2011 00:34
Show Gist options
  • Save fxg42/1474646 to your computer and use it in GitHub Desktop.
Save fxg42/1474646 to your computer and use it in GitHub Desktop.
JFreeChart with Kendo style
@Grab('org.jfree:jfreechart:1.0.14')
import java.awt.*
import java.awt.geom.*
import java.io.*
import java.text.*
import org.jfree.chart.*
import org.jfree.chart.annotations.*
import org.jfree.chart.axis.*
import org.jfree.chart.block.*
import org.jfree.chart.labels.*
import org.jfree.chart.plot.*
import org.jfree.chart.renderer.category.*
import org.jfree.chart.util.*
import org.jfree.data.category.*
import org.jfree.ui.*
//
// Test script to see if I can make a JFreeChart bar chart look like Kendo's.
// http://demos.kendoui.com/dataviz/bar-charts/column.html
// Results at:
// http://imgur.com/a/RGLqx
//
series= [
[ "name": "World",
"data": [2005:15.7, 2006:16.7, 2007:20, 2008:23.5, 2009:26.6]
],
[ "name": "United States",
"data": [2005:67.96, 2006:68.93, 2007:75, 2008:74, 2009:78]
],
]
// Initialize a dataset.
dataset = new DefaultCategoryDataset()
series.each { serie ->
serie.data.each { category, value ->
dataset.addValue(value, serie.name, category)
}
}
// Create the bar chart with default style.
barchart = ChartFactory.createBarChart("Internet Users","","",dataset,PlotOrientation.VERTICAL,true,false,false)
// Render the default chart to a file.
file = new FileOutputStream("default.png")
ChartUtilities.writeChartAsPNG(file, barchart, 550, 420)
file.close()
// Change the title font
barchart.title.font = new Font("Arial", Font.PLAIN, 16)
// ... and color.
barchart.title.paint = new Color(142, 142, 142)
// Remove the frame around the legend.
barchart.legend.frame = BlockBorder.NONE
// Adjust the padding around the entire chart.
barchart.padding = new RectangleInsets(10f, 0f, 10f, 0f)
// Change the default background color from gray to white.
barchart.plot.backgroundPaint = Color.white
// Change the default gridline color
barchart.plot.rangeGridlinePaint = new Color(223, 223, 223)
// ... and line style
barchart.plot.rangeGridlineStroke = new BasicStroke()
// Remove the chart frame
barchart.plot.outlineVisible = false
// ... and almost all axis margin.
barchart.plot.axisOffset = new RectangleInsets(0f, 1f, 0f, 0f)
// Drop some shadows under the columns.
barchart.plot.shadowGenerator = new DefaultShadowGenerator()
// Adjust the margin between columns in same year (category)
barchart.plot.renderer.itemMargin = 0.09
// ...and the margin between the years (categories).
barchart.plot.domainAxis.categoryMargin = 0.30
// Change the series' background and border color.
barchart.plot.renderer.setSeriesPaint(0, new Color(255, 104, 0))
barchart.plot.renderer.setSeriesOutlinePaint(0, new Color(204, 83, 0))
barchart.plot.renderer.setSeriesPaint(1, new Color(160, 167, 0))
barchart.plot.renderer.setSeriesOutlinePaint(1, new Color(128, 134, 0))
// ...and set a gradient.
barchart.plot.renderer.barPainter = new KendoGradientBarPainter()
// Format the y-axis labels as percentages
format = new DecimalFormat("##")
format.positiveSuffix = "%"
// ... on the y-axis
barchart.plot.rangeAxis.tickUnit = new NumberTickUnit(10, format)
// ... and in the columns.
barchart.plot.renderer.baseItemLabelGenerator = new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, format)
// Make the labels inside the columns visible
barchart.plot.renderer.baseItemLabelsVisible = true
// ... rotate them
barchart.plot.renderer.basePositiveItemLabelPosition = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, -Math.PI/2)
// ... and change their color.
barchart.plot.renderer.setSeriesItemLabelPaint(0, Color.white)
barchart.plot.renderer.setSeriesItemLabelPaint(1, Color.white)
// Render the chart to another file.
file = new FileOutputStream("kendo.png")
ChartUtilities.writeChartAsPNG(file, barchart, 550, 420)
file.close()
// Subclass of JFreeChart's GradientBarPainter to make the column gradients look
// like Kendo's.
// See: http://www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/chart/renderer/category/GradientBarPainter.html#line.104
class KendoGradientBarPainter extends GradientBarPainter {
public KendoGradientBarPainter () { super() }
public KendoGradientBarPainter(double g1, double g2, double g3) { super(g1, g2, g3) }
@Override public void paintBar (Graphics2D g2, BarRenderer renderer, int row, int column, RectangularShape bar, RectangleEdge base) {
def c0 = renderer.getItemPaint(row, column)
def factor = 1.2
def c1 = new Color(Math.min(255,c0.red*factor as int), Math.min(255, c0.green*factor as int), Math.min(255,c0.blue * factor as int))
def regions = split(bar, 0.25)
def gp = new GradientPaint((float) regions[0].getMinX(), 0.0f, c0, (float) regions[0].getMaxX(), 0.0f, c1)
g2.setPaint(gp)
g2.fill(regions[0])
gp = new GradientPaint((float) regions[1].getMinX(), 0.0f, c1, (float) regions[1].getMaxX(), 0.0f, c0)
g2.setPaint(gp)
g2.fill(regions[1])
def stroke = renderer.getItemOutlineStroke(row, column)
def paint = renderer.getItemOutlinePaint(row, column)
if (stroke && paint) {
g2.setStroke(stroke)
g2.setPaint(paint)
g2.draw(bar)
}
}
def split (bar, a) {
def result = new Rectangle2D[2]
double x0 = bar.getMinX()
double x1 = Math.rint(x0 + (bar.getWidth() * a))
result[0] = new Rectangle2D.Double(bar.getMinX(), bar.getMinY(), x1 - x0, bar.getHeight())
result[1] = new Rectangle2D.Double(x1, bar.getMinY(), bar.getMaxX() - x1, bar.getHeight())
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment