How to add colorLine to chart legend

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

How to add colorLine to chart legend

Post by Jonathan » Mon May 20, 2013 4:25 pm

Hi,

I have a simple bar chart and I draw a line using ColorLine object to set a limit. (I attached an image to illustrate my point) Is there a way to add the colorline to chart legend? I couldn't find any API to do that. If not, what is alternative way to achieve this?

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		panel.add(chart);
		
		StringList labelsList = new StringList(3);
		labelsList.add(0, "1.11");
		labelsList.add(1, "2.22");
		labelsList.add(2, "3.33");
		
		Bar blueSeries = new Bar(chart.getChart());
		blueSeries.setMultiBar(MultiBars.STACKED);
		blueSeries.add(0, 10, "A");
		blueSeries.add(1, 20, "B");
		blueSeries.add(2, 30, "C");
		blueSeries.getMarks().setVisible(true);
		blueSeries.setTitle("blue series");
		blueSeries.setLabels(labelsList);
		blueSeries.getMarks().getFont().setSize(20);		// size
		blueSeries.getMarks().getFont().setColor(Color.red);		// color
		blueSeries.getMarks().getFont().setBold(true);	//bold
		blueSeries.getMarks().getFont().setItalic(true);	//italic
		blueSeries.getMarks().getFont().setName("");// style

		ColorLine line = new ColorLine(chart.getChart());
		line.getPen().setColor(Color.RED);
		line.setAxis(chart.getAxes().getLeft());
		line.setValue(15);
		
		//custom x label
		chart.getAxes().getBottom().getCustomLabels().clear();
		chart.getAxes().getBottom().getCustomLabels().add(0.0, "A");
		chart.getAxes().getBottom().getCustomLabels().add(1.0, "B");
		chart.getAxes().getBottom().getCustomLabels().add(2.0, "C");
		
		chart.getAspect().setView3D(false);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}
Attachments
bar chart with a limit line.png
bar chart with a limit line.png (7.73 KiB) Viewed 28278 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: How to add colorLine to chart legend

Post by Narcís » Wed May 22, 2013 10:54 am

Hi Jonathan,

I'm not sure about what would you like to get at the legend exactly. However, most options would involve custom drawing on the chart canvas. You could draw lines on the legend as explained in Tutorial 10 - Custom drawing on the Chart Panel. If this doesn't help please provide more detailed information about your exact needs.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: How to add colorLine to chart legend

Post by Jonathan » Wed May 22, 2013 2:55 pm

Hi,

I have cases where I have multiple colorlines on a bar chart. Users want to add labels on each colorline on chart legend if possible. I don't know if I miss it on the tutorial page, but I can't find a way to add new custom labels on chart legend. For example, as I attached a sample code and image, there are two colorlines; red and blue. I want to add a label of "low limit" for the red line and another label of "high limit" for the blue line to the chart legend. Ideally, I want to see the following on the chart legend:
----------------------------------
blue-box-image 10 10
blue-box-image 20 20
blue-box-image 30 30
red-line-image low limit
blue-line-image high limit
----------------------------------

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		panel.add(chart);
		
		StringList labelsList = new StringList(3);
		labelsList.add(0, "10");
		labelsList.add(1, "20");
		labelsList.add(2, "30");
		
		Bar blueSeries = new Bar(chart.getChart());
		blueSeries.setMultiBar(MultiBars.STACKED);
		blueSeries.add(0, 10, "A");
		blueSeries.add(1, 20, "B");
		blueSeries.add(2, 30, "C");
		blueSeries.getMarks().setVisible(true);
		blueSeries.setTitle("blue series");
		blueSeries.setLabels(labelsList);
		blueSeries.getMarks().getFont().setSize(20);		// size
		blueSeries.getMarks().getFont().setColor(Color.red);		// color
		blueSeries.getMarks().getFont().setBold(true);	//bold
		blueSeries.getMarks().getFont().setItalic(true);	//italic
		blueSeries.getMarks().getFont().setName("");// style

		ColorLine line = new ColorLine(chart.getChart());
		line.getPen().setColor(Color.RED);
		line.setAxis(chart.getAxes().getLeft());
		line.setValue(15);
		
		ColorLine line2 = new ColorLine(chart.getChart());
		line2.getPen().setColor(Color.BLUE);
		line2.setAxis(chart.getAxes().getLeft());
		line2.setValue(30);
		
		//custom x label
		chart.getAxes().getBottom().getCustomLabels().clear();
		chart.getAxes().getBottom().getCustomLabels().add(0.0, "A");
		chart.getAxes().getBottom().getCustomLabels().add(1.0, "B");
		chart.getAxes().getBottom().getCustomLabels().add(2.0, "C");
		
		chart.getAspect().setView3D(false);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}
Attachments
bar chart with multiple limit lines.png
bar chart with multiple limit lines.png (8.02 KiB) Viewed 28234 times

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: How to add colorLine to chart legend

Post by Yeray » Thu May 23, 2013 10:35 am

Hi,

There's a trick to do this. You could hide all your series from the legend and add as many series as items you wish in the legend. Then you can set these series titles and colors. Ie:

Code: Select all

	      blueSeries.setShowInLegend(false);
	      
	      Bar legendItem1 = new Bar(chart.getChart());
	      Bar legendItem2 = new Bar(chart.getChart());
	      Bar legendItem3 = new Bar(chart.getChart());
	      legendItem1.setColor(blueSeries.getColor());
	      legendItem2.setColor(blueSeries.getColor());
	      legendItem3.setColor(blueSeries.getColor());
	      legendItem1.setTitle("10 10");
	      legendItem2.setTitle("20 20");
	      legendItem3.setTitle("30 30");
	      
	      Line legendItem4 = new Line(chart.getChart());
	      Line legendItem5 = new Line(chart.getChart());
	      legendItem4.setColor(line.getPen().getColor());
	      legendItem5.setColor(line2.getPen().getColor());
	      legendItem4.setTitle("low limit");
	      legendItem5.setTitle("high limit");
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: How to add colorLine to chart legend

Post by Jonathan » Fri May 24, 2013 3:41 pm

Cool. Thanks. I like the trick. :wink:

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: How to add colorLine to chart legend

Post by Jonathan » Fri May 24, 2013 8:49 pm

Hi,

I need another trick. ;)

Although the following problem could be a different problem, I am asking you here since it is regarding to add labels on chart legend.
Problem: Symbols for labels get changed to rectangle instead of line if I have following lines of codes.

Code: Select all

		chart.getAspect().setView3D(true);
		chart.getAspect().setChart3DPercent(0);
I can't remove this code because it was there to fix another TeeChart bug, where the median line in boxes overlap the right edge of the box. I attached a image to describe the problems.
Question: Is there a way to fix both bugs at the same time?

I pasted two different codes:
Code1: Here is the code where line label symbols are showing correctly but the median line overlap the right edge of the box.

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		panel.add(chart);
		
		Box boxSeries = new Box(chart.getChart());
		boxSeries.add(0, 10, "A");
		boxSeries.add(1, 20, "B");
		boxSeries.add(2, 30, "C");
		boxSeries.setMedian(18.5);
		boxSeries.getMedianPen().setVisible(true);
		boxSeries.getMedianPen().setStyle(DashStyle.DASH);
		boxSeries.getMedianPen().setWidth(2);
		
		ColorLine line = new ColorLine(chart.getChart());
		line.getPen().setColor(Color.RED);
		line.setAxis(chart.getAxes().getLeft());
		line.setValue(15);
		
		ColorLine line2 = new ColorLine(chart.getChart());
		line2.getPen().setColor(Color.BLUE);
		line2.setAxis(chart.getAxes().getLeft());
		line2.setValue(30);
        
		Line line3 = new Line(chart.getChart());
		line3.setTitle("one");
		
		Line line4 = new Line(chart.getChart());
		line4.setTitle("two");
		
		chart.getAspect().setView3D(false);
//		chart.getAspect().setView3D(true);
//		chart.getAspect().setChart3DPercent(0);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}
Code2: Here is the code where line label symbols are showing INcorrectly but the median line issue gets fixed.

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		panel.add(chart);
		
		Box boxSeries = new Box(chart.getChart());
		boxSeries.add(0, 10, "A");
		boxSeries.add(1, 20, "B");
		boxSeries.add(2, 30, "C");
		boxSeries.setMedian(18.5);
		boxSeries.getMedianPen().setVisible(true);
		boxSeries.getMedianPen().setStyle(DashStyle.DASH);
		boxSeries.getMedianPen().setWidth(2);
		
		ColorLine line = new ColorLine(chart.getChart());
		line.getPen().setColor(Color.RED);
		line.setAxis(chart.getAxes().getLeft());
		line.setValue(15);
		
		ColorLine line2 = new ColorLine(chart.getChart());
		line2.getPen().setColor(Color.BLUE);
		line2.setAxis(chart.getAxes().getLeft());
		line2.setValue(30);
        
		Line line3 = new Line(chart.getChart());
		line3.setTitle("one");
		
		Line line4 = new Line(chart.getChart());
		line4.setTitle("two");
		
//		chart.getAspect().setView3D(false);
		chart.getAspect().setView3D(true);
		chart.getAspect().setChart3DPercent(0);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}
Attachments
line symbols get changed on 2 dimention box chart.png
line symbols get changed on 2 dimention box chart.png (19.74 KiB) Viewed 28239 times

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: How to add colorLine to chart legend

Post by Yeray » Mon May 27, 2013 2:47 pm

Hi,
Jonathan wrote:Problem: Symbols for labels get changed to rectangle instead of line if I have following lines of codes.
Use FastLine series instead of Line series. The Line series symbol changes because in 2D it's a line while in 3D it has a depth, it's like an area. However, the FastLine series is always a thin line.
Jonathan wrote:another TeeChart bug, where the median line in boxes overlap the right edge of the box
You're right. I'll add it to the defect list to be revised for future releases (TJ71016583).
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: How to add colorLine to chart legend

Post by Jonathan » Tue May 28, 2013 7:35 pm

Hi Yeray,

Thanks. FastLine works in this case.

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: How to add colorLine to chart legend

Post by Jonathan » Mon Jun 10, 2013 4:34 pm

Hi,

I need one more help. Hopefully, this is the last one. ;)

Question: How to display the colorLine behind the chart legend and in front of data?

In the following sample code, I have two colorLines (one is in blue and the other is in red), and I place a chart legend with custom positions. By default, colorLines are top of the chart legend, just like blue line in my sample code below. I don't want to use the following setting (setDrawBehind) because if so, the colorline like red line in my code is rendered behind my data. Is there a way to display the colorLine in front of data, but behind the chart legend? In other words, Is there a way to display the chart legend on top of everything without changing the default behavior of colorLine's object?

Code: Select all

line.setDrawBehind(true);

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		panel.add(chart);
		
		Box boxSeries = new Box(chart.getChart());
		boxSeries.add(0, 10, "A");
		boxSeries.add(1, 20, "B");
		boxSeries.add(2, 30, "C");
		boxSeries.setColor(Color.BLACK);
		
		ColorLine line = new ColorLine(chart.getChart());
		line.getPen().setColor(Color.RED);
		line.setAxis(chart.getAxes().getLeft());
		line.setValue(15);
		line.setDrawBehind(true);
		
		ColorLine line2 = new ColorLine(chart.getChart());
		line2.getPen().setColor(Color.BLUE);
		line2.setAxis(chart.getAxes().getLeft());
		line2.setValue(20);
        
		FastLine line3 = new FastLine(chart.getChart());
		line3.setTitle("one");
		
		FastLine line4 = new FastLine(chart.getChart());
		line4.setTitle("two");
		
		chart.getAspect().setView3D(false);
		
		// set legend with custom position
		chart.getLegend().setCustomPosition(true);
		chart.getLegend().setLeft(100);
		chart.getLegend().setTop(120);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}
Attachments
colorline is top of legend.png
colorline is top of legend.png (7.56 KiB) Viewed 28165 times

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: How to add colorLine to chart legend

Post by Yeray » Thu Jun 13, 2013 10:17 am

Hi,

I can't think on a way to force the Legend to be drawn on top of all the other elements right now. I've added it to the wish list to be implemented in future releases (TJ71016602).
In the meanwhile, the only way I an think on would be hiding the legend and drawing it manually using custom drawing techniques at the AfterDraw event.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply