Topic: Regarding Bar Chart Representation

Hi everyone,

I created a bar chart using this script example (This one: https://myvisualdatabase.com/forum/view … ?id=5340), and it works. I have data for months and income. Currently, I'm using numbers instead of month names to represent the correct month. Is there any way to use strings instead of floats or dates in bar charts?

Many thanks!



ChartBar := TChart.Create(Main);
  ChartBar.Parent := Main.LevelChart;
  ChartBar.Align := alClient;
  ChartBar.AddSeries(TBarSeries.Create(ChartBar));


ChartBar.Series[0].AddXY(Month,Value);

Re: Regarding Bar Chart Representation

Any comments?

Re: Regarding Bar Chart Representation

You mean getting the names of the month on the X axle  instead of the 1 to 12 ?

4 (edited by sparrow 2024-04-02 19:45:58)

Re: Regarding Bar Chart Representation

sonixax wrote:

Any comments?


In the example you indicated:
Update the ChartBar creation code part:

  ChartBar := TChart.Create(Form1);
  ChartBar.Parent := Form1.PanelBar;
  ChartBar.Align := alClient;
  ChartBar.AddSeries(TBarSeries.Create(ChartBar));
  ChartBar.Series[0].XValues.DateTime := true;
  TBarSeries(ChartBar.Series[0]).marks.Style:=smsValue;
  Form1.bBarUpdate.Click;

Update procedure Form1_bBarUpdate_OnClick

procedure Form1_bBarUpdate_OnClick (Sender: string; var Cancel: boolean);
var
    Results: TDataSet;
    DateValue: Double;
    QtyValue, Lab: string;
begin
    ChartBar.Series[0].Clear;

    SQLQuery('SELECT date, qty, CASE strftime(''%d'', date)*1 WHEN 1 THEN ''Jan'' WHEN 2 THEN ''Feb'' ELSE ''Other'' END AS Lab FROM bar_data ORDER BY date DESC', Results);

    while not Results.Eof do
    begin
        DateValue := SQLDateTimeToDateTime( Results.FieldByName('date').asString );
        QtyValue := Results.FieldByName('qty').asString;
        Lab := Results.FieldByName('Lab').asString;

        if ValidInt(QtyValue) then ChartBar.Series[0].AddXY(DateValue, StrToInt(QtyValue), Lab);
        Results.Next;
    end;

end;

On the X axis we now display months as an example.
https://myvisualdatabase.com/forum/misc.php?action=pun_attachment&item=10361

Post's attachments

Attachment icon ex.jpg 32.84 kb, 4 downloads since 2024-04-02 

Re: Regarding Bar Chart Representation

You can also use this form for the Sql Query with sqlite:

 SQLQuery('select substr('JanFebMarAprMayJunJulAugSepOctNovDec', 1 + 3*strftime('%m', date), -3) AS Lab FROM bar_data ORDER BY date DESC', Results)

;