# EasyChart legend: increase key line width and change colours

**URL:** <https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337>\
**Category:** Ignition\
**Created:** [September 14, 2017, 12:42am UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337 "2017-09-14T00:42:31Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 14, 2017, 12:42am UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/1 "2017-09-14T00:42:31Z")

</div>

Sorry for the potentially simple question (my java knowledge is very basic). I’ve looked through the API for the PMIEasyChart and JFreeChart, but am more or less fumbling around…

Is it possible to increase the width of the lines in the key legend on the EasyChart - without increasing the pen line width - via python?  
I’d like to keep the pen width set to 1, but set the corresponding legend item to 2, for example, to help to distinguish colours apart.

Similarly, is it possible to change the legend background colour to match the background colour in the chart area, and the pen label font colour ?

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c2e5a77a231300443e311d78cf8c147061c3aeab.png)

Cheers

---

<div class="post-metadata">

**Author:** ![hamlinjr](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hamlinjr/32/8228_2.png) [@hamlinjr](https://forum.inductiveautomation.com/u/hamlinjr)\
**Post date:** [September 14, 2017, 1:01pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/2 "2017-09-14T13:01:23Z")

</div>

While someone probably has a cool scripting solution for you, here’s the poor man’s version I’ve sucessfully used on one of our dashboards: hide the legend and use labels instead. Example attached.

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/35676c6d19a3010675c17cbffa09fe021cbd8adc.JPG)

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [September 14, 2017, 3:22pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/3 "2017-09-14T15:22:46Z")

</div>

```auto
	from java.awt import BasicStroke,Font
	
	plot = chart.getPlot()
	renderer = plot.getRenderer()
	
	renderer.setBaseLegendTextFont(Font("Arial",Font.BOLD,16))
	
	seriesIndex = 0
	lineWidth = 4
	renderer.setSeriesStroke(seriesIndex,BasicStroke(lineWidth))

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 14, 2017, 3:31pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/4 "2017-09-14T15:31:08Z")

</div>

Jae’s is closer to what you want, but for the sake of it, here’s as far as I got without modifying the line actually drawn on the chart:

```auto
	renderer = chart.getPlot().getRenderer()
	shape = renderer.getLegendLine()
	
	rect = shape.getBounds2D()
	rect.setRect(shape.getX1(), shape.getY1(), rect.getWidth(), rect.getHeight() + 4)
	
	renderer.setLegendLine(rect)

```

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/5ca429754ba7f0f36960e39bb361456a8468e3f4.png)

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [September 14, 2017, 3:45pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/5 "2017-09-14T15:45:14Z")

</div>

Oops, didn’t check to see that it modified the graph’s width too.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 14, 2017, 7:24pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/6 "2017-09-14T19:24:43Z")

</div>

Well, that ended up being _way_ more involved than I figured it would be. But because I enjoyed myself:  
(Preemptive disclaimer: Don’t even think of trying to call in to support if this doesn’t work for you)

```auto
	from org.jfree.chart.renderer.xy import XYLineAndShapeRenderer
	from org.jfree.chart import LegendItem
	legend = chart.getLegend()
	plot = chart.getPlot()
	renderer = plot.getRenderer()
	
	class CustomRenderer(XYLineAndShapeRenderer):
		def getLegendItem(self, dataset, series):
			original = renderer.getLegendItem(dataset, series)
			new = LegendItem(
				original.getLabel(),
				original.getDescription(),
				original.getToolTipText(),
				"", #urlText
				original.getShape(),
				original.getLinePaint(), #set fill paint to line paint
				original.getOutlineStroke(),
				original.getOutlinePaint()
			)
			new.setSeriesIndex(series)
			new.setDatasetIndex(dataset)
			return new
		
		def getLegendItems(self):
			collection = XYLineAndShapeRenderer.getLegendItems(self)
			for ds in range(plot.getDatasetCount()):
				for series in range(plot.getSeriesCount()):
					legend = self.getLegendItem(ds, series)
					collection.add(legend)
			return collection
			
	newRenderer = CustomRenderer(True, False)
	newRenderer.setBaseSeriesVisibleInLegend(True)
	legend.setSources([newRenderer])

```

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/7/7a55b3022738415a3c5c901cb0d0c06a3467f4b1.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 15, 2017, 3:19pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/7 "2017-09-15T15:19:07Z")

</div>

> [@paul-griffith](#):
>
> (Preemptive disclaimer: Don't even think of trying to call in to support if this doesn't work for you)

That was a chuckle I needed. 😆

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 15, 2017, 4:13pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/8 "2017-09-15T16:13:24Z")

</div>

Oh, yeah, and to answer the other questions about tying the legend background to the chart background, add this:

```auto
	legend.setBackgroundPaint(self.background)
	legend.setItemPaint(self.foreground)

```

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1dde49e43f81024745bf0f3c640e4471600cdae9.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 18, 2017, 5:14am UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/9 "2017-09-18T05:14:36Z")

</div>

Hi Paul,

Firstly thanks a lot for your time, I didn’t realise that it would be so involved!

Where do I need to add this script to? It looks like you defined in within a custom method (due to ‘self’), or a script library. If so, what parameters do I need to define?

I’ve looked in the API in the definitions for PMIEasyChart and PMIChart and can’t find the method getPlot() (PMIChart has getPlots()) so I’m curious how you found this? I’m still very new to Python and Java…

Cheers again

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 18, 2017, 10:11pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/10 "2017-09-18T22:11:23Z")

</div>

This code would be a drop in on the `configureChart` extension function; if you are trying to fire it from an external source it takes more coercing to get the root `JFreeChart` object.

As for the code itself: It’s a combination of a lot of diving into the [JFreeChart API manual](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/plot/XYPlot.html) and knowledge that I’ve acquired by brute forcing/shamelessly copying examples posted elsewhere online.

---

<div class="post-metadata">

**Author:** ![joeyjojoshabadu](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@joeyjojoshabadu](https://forum.inductiveautomation.com/u/joeyjojoshabadu)\
**Post date:** [November 24, 2017, 7:40pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/11 "2017-11-24T19:40:15Z")

</div>

I also removed the legend border:

```auto
	from org.jfree.chart.block import BlockBorder 
	chart.legend.setFrame(BlockBorder.NONE)

```

---

<div class="post-metadata">

**Author:** ![carolina\_elfeky](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@carolina\_elfeky](https://forum.inductiveautomation.com/u/carolina_elfeky)\
**Post date:** [April 16, 2018, 2:26pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/12 "2018-04-16T14:26:30Z")

</div>

I’ve been trying to change the position of the Legend from the bottom to the right and I just can’t get it to work. if I do the “.getPosition()” it will give me the current position “RectangleEdge.BOTTOM”, but if I try to use the “.setPosition” I get the following error:  
‘org.jfree.chart.title.LegendTitle’ object has no attribute ‘setPostion’  
I’m super new to JAVA to be honest.

Here’s the code

```
from java.awt import Color,BasicStroke,Font
from org.jfree.chart import LegendItem

plot = chart.getPlot()
legend = chart.getLegend()

print legend.getPosition()
legend.setPostion(RectangleEdge.RIGHT)
```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 16, 2018, 2:33pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/13 "2018-04-16T14:33:15Z")

</div>

Typo in setPosition(). You’re missing an ‘i’.

---

<div class="post-metadata">

**Author:** ![carolina\_elfeky](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@carolina\_elfeky](https://forum.inductiveautomation.com/u/carolina_elfeky)\
**Post date:** [April 16, 2018, 2:44pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/14 "2018-04-16T14:44:12Z")

</div>

Well, that’s embarrassing…Still getting this error.

global name ‘RectangleEdge’ is not defined

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 16, 2018, 2:45pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/15 "2018-04-16T14:45:30Z")

</div>

> [@carolina\_elfeky](#):
>
> global name 'RectangleEdge' is not defined

You need to import RectangleEdge.

---

<div class="post-metadata">

**Author:** ![carolina\_elfeky](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@carolina\_elfeky](https://forum.inductiveautomation.com/u/carolina_elfeky)\
**Post date:** [April 16, 2018, 2:48pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/16 "2018-04-16T14:48:35Z")

</div>

Thanks for your help! Finally got it. 🙂

---

<div class="post-metadata">

**Author:** ![KA4HRE](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ka4hre/32/42145_2.png) [@KA4HRE](https://forum.inductiveautomation.com/u/KA4HRE)\
**Post date:** [October 28, 2020, 6:03pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/17 "2020-10-28T18:03:19Z")

</div>

This works great with a single subplot, but fails when a second subplot is added.  
I’ll browse the JFreeChart API manual to see if I can make it work with two subplots, but in case someone sees this that has already figured this out, that would save me some time!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 28, 2020, 6:10pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/18 "2020-10-28T18:10:30Z")

</div>

Use a technique like this to account for each subplot:  
[http://forum.inductiveautomation.com/t/labels-for-data-points-in-chart/14740/5?u=pgriffith](http://forum.inductiveautomation.com/t/labels-for-data-points-in-chart/14740/5)

---

<div class="post-metadata">

**Author:** ![KA4HRE](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ka4hre/32/42145_2.png) [@KA4HRE](https://forum.inductiveautomation.com/u/KA4HRE)\
**Post date:** [October 28, 2020, 9:57pm UTC](https://forum.inductiveautomation.com/t/easychart-legend-increase-key-line-width-and-change-colours/15337/19 "2020-10-28T21:57:16Z")

</div>

Thanks! I’ll give this a try!
