Recent

Author Topic: Lazarus TCharts  (Read 7336 times)

vks

  • New Member
  • *
  • Posts: 33
Lazarus TCharts
« on: April 14, 2015, 07:56:54 am »
Hello,

I am using Lazarus v1.2.6.
I generated a chart with its Marks.Style as 'smsLabel' by setting it in object inspector.
so for all bars generated, its showing the label as it is in the picture I have attached.
but for those bars whose 'Y axis' value is zero, the label must not be displayed.
How do I achieve this?

wp

  • Hero Member
  • *****
  • Posts: 13417
Re: Lazarus TCharts
« Reply #1 on: April 14, 2015, 10:09:09 am »
Sorry, there are no bar labels in your screenshot.

How do you assign the labels to the data points? As a parameter in the Add method? Then why don't you assign labels only to those data points which should display them?

vks

  • New Member
  • *
  • Posts: 33
Re: Lazarus TCharts
« Reply #2 on: April 14, 2015, 10:40:34 am »
Hello

I am so sorry for attaching the wrong image.
Here is how my graph looks

wp

  • Hero Member
  • *****
  • Posts: 13417
Re: Lazarus TCharts
« Reply #3 on: April 14, 2015, 11:37:05 am »
As I said, assign the labels only where needed. Something like this:
Code: [Select]
procedure TForm1.PopulateSeries;
const
  LABELVALUES:array[1..3] = ('small', 'medium', 'big');
var
  i: Integer;
  labeltext: String;
  y: Integer;
begin
  for i:=0 to 10 do
  begin
    y := random(3); // random data for y, some of the values are 0
    if y = 0 then labelText := '' else labelText := LABELVALUES[y];
    Chart1BarSeries1.AddXY(i, y, labelText);
  end;
end;

vks

  • New Member
  • *
  • Posts: 33
Re: Lazarus TCharts
« Reply #4 on: April 14, 2015, 11:48:24 am »
But without setting the Marks.Style as smsLabel , the labels won't be displayed.
If smsLabel is set, its being displayed for all bars.

wp

  • Hero Member
  • *****
  • Posts: 13417
Re: Lazarus TCharts
« Reply #5 on: April 14, 2015, 12:35:25 pm »
No, you must be doing something wrong - run attached demo which shows labels only at the non-zero bars.

vks

  • New Member
  • *
  • Posts: 33
Re: Lazarus TCharts
« Reply #6 on: April 14, 2015, 12:44:29 pm »
Thanks a lot wp.. It worked!!

wp

  • Hero Member
  • *****
  • Posts: 13417
Re: Lazarus TCharts
« Reply #7 on: April 14, 2015, 01:00:40 pm »
Why are there two labels at the center bar?

vks

  • New Member
  • *
  • Posts: 33
Re: Lazarus TCharts
« Reply #8 on: April 14, 2015, 01:14:43 pm »
Because there are 2 C++ trainings scheduled on 13th of January.

wp

  • Hero Member
  • *****
  • Posts: 13417
Re: Lazarus TCharts
« Reply #9 on: April 14, 2015, 03:23:27 pm »
Do you know that you can also stack several sets of y data on top of each other and color them differently? If not have a look at this tutorial: http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Stacked_BarSeries

vks

  • New Member
  • *
  • Posts: 33
Re: Lazarus TCharts
« Reply #10 on: April 14, 2015, 03:33:24 pm »
That was really helpful  :) :)
and how to rename x-axis values?
this method:
Chart1BarSeries1.AddXY(x,y,'some_txt',color)
it takes x and y values as double!! the same is displayed in the graph.
what if i want to replace x-axis coordinates with some text?

wp

  • Hero Member
  • *****
  • Posts: 13417
Re: Lazarus TCharts
« Reply #11 on: April 14, 2015, 05:10:50 pm »
Use a separate TListChartSource to which you can add the labels, use the same x coordinates that you have in the series. Then assign the ListChartSource to Marks.Source of the x axis. The basic steps are described in the other barchart tutorial http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_BarSeries (using the built-in listsource of the series). The only difference is that you populate the listseries by code:
Code: [Select]
var
  startcode: TDate;
  x: TDate;
  i, y: Integer;
  xlabel, ylabel: String;
for i:=0 to 10 do
begin
  x := startdate + i;
  xLabel := FormatDateTime('dd/mm', x);
  ChartListSource1.Add(x, x, xLabel);  // the 2nd x is the y value which would be needed if the x/y axes would be interchanged - here you could use any value for the 2nd x
  y := random(2);
  if y = 0 then yLabel := '' else yLabel := (...something...);
  Chart1BarSeries1.AddXY(x, y, yLabel); 
end;
As for the stacked series, you need multiple y values in the bar series. This makes assigning labels rather tricky, and I currently don't have a solution, I probably have to modify the event OnGetMark.

But your screen shots give the impression that you don't need individual point labels but the series title which can be displayed in the legend - just follow the tutorial mentioned in the previous post.

vks

  • New Member
  • *
  • Posts: 33
Re: Lazarus TCharts
« Reply #12 on: April 15, 2015, 02:02:11 pm »
Thanks lot wp!!  :) :) :) :)

 

TinyPortal © 2005-2018