Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple y-axes on a graphing/charting component

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi,

At the moment I have a TChart component with 1 x-axis and 1 y-axis on which I display 3 series. Each series has a different scale (e.g. Series1 0..5, Series2 0..2, Series3 0..1). Ideally, what I want to do is display 3 y-axes and 1 x-axis so that each y-axis has the relevant scale for its particular Series (if that makes sense!!!). Any suggestions?

P.S. Below is a crude little diagram!!
|||
|||
|||
3 y-axes --> |||
|||________________ <-- 1 x-axis


 
try Chart.ChartGetAxisLabel, this will give you the possibility to modify the axis labels.
Then you can write all 3 y axis texts.

This is not nice but as far as i know you can only have one y axis in a TChart.

Roderich
 
hi

I think 2 y axis is the max. I have used a TDBChart(derives from TChart) and added 2 series to one graph. I think if you want to add more than 3 you have to assign them to one of the Y axis and it adjusts to a range suitable for both series.

I've copied and pasted my proc which does this:

procedure TBase.AddSeriesToGraph(sLabel : string; xSelField : Tfield; ySelField : Tfield);
var NewSeries : TLineSeries;
MsgRes : TModalResult;
begin
Application.ProcessMessages;
NewSeries := TLineSeries.Create(self);
NewSeries.ParentChart := OvlGraph.Chart;
NewSeries.DataSource := Query;
NewSeries.CheckDataSource;
NewSeries.LinePen.Width := 2;
NewSeries.SeriesColor := RGB(Random(256), Random(256),Random(256));

OvlGraph.Chart.RightAxis.Increment := 0.0001;
OvlGraph.Chart.LeftAxis.Increment := 0.0001;

if OvlGraph.Chart.SeriesCount > 2 then
begin
MsgRes := MessageDlg('Scale to Left Axis or Right Axis ? (Yes = Left, No = Right)',
mtConfirmation,[mbyes,mbno, mbCancel],0);
if MsgRes = MRCancel then
begin
NewSeries.Free;
Exit;
end;
if MsgRes = mrYes then
begin
NewSeries.VertAxis := aLeftAxis;
NewSeries.YValues.ValueSource := ySelField.FieldName;
end
else
begin //result = mrNo
NewSeries.VertAxis := aRightAxis;
OvlGraph.Chart.RightAxis.LabelsOnAxis := true;
NewSeries.YValues.ValueSource := ySelField.FieldName;
end;
Application.ProcessMessages;
end;

if OvlGraph.Chart.SeriesCount = 1 then
begin
NewSeries.VertAxis := aLeftAxis;
NewSeries.YValues.ValueSource := ySelField.FieldName;
OvlGraph.Chart.LeftAxis.Title.Caption := ySelField.FieldName;
NewSeries.SeriesColor := RGB(Random(100)+100, Random(100)+100,Random(100)+100);
Application.ProcessMessages;
end;

if OvlGraph.Chart.SeriesCount = 2 then
begin
NewSeries.SeriesColor := RGB(Random(100)+20, Random(100)+20,Random(100)+20);
if OvlGraph.Chart.RightAxis.Title.Caption = '' then
begin
NewSeries.VertAxis := aRightAxis;
OvlGraph.Chart.RightAxis.LabelsOnAxis := true;
NewSeries.YValues.ValueSource := ySelField.FieldName;
OvlGraph.Chart.RightAxis.Title.Caption := ySelField.FieldName;
end
else //insert 2nd one on the left
begin
NewSeries.VertAxis := aLeftAxis;
OvlGraph.Chart.LeftAxis.LabelsOnAxis := true;
NewSeries.YValues.ValueSource := ySelField.FieldName;
OvlGraph.Chart.LeftAxis.Title.Caption := ySelField.FieldName;
end;
Application.ProcessMessages;
end;

if xSelField.DataType = ftDateTime
then NewSeries.XValues.DateTime := true;
NewSeries.XValues.ValueSource := xSelField.FieldName;
OvlGraph.Chart.BottomAxis.Title.Caption := xSelField.FieldName;
OvlGraph.Chart.BottomAxis.Automatic := true;
OvlGraph.Chart.BottomAxis.AdjustMaxMin;
NewSeries.Title := ySelField.FieldName;

SynchRelativeScales;
Application.ProcessMessages;
OvlGraph.Chart.repaint;
Application.ProcessMessages;
end;

Hope this is helpful.

lou
 
Feel free to tidy up and post back :->. It was my first attempt at graphs and it works, so I haven't looked at it since...
 
Roderich:

I can't find either
Code:
 Chart.ChartGetAxisLabel
or
Code:
 Chart.GetAxisLabel
I don't think it will really help to simply have 3 labels because the 3 series will be assigned to the one axis still, which means 2 graphs will be very tiny and one will be the right size (they won't be resized according to a specific axis!).

Lou:

Thanks for the muchos code you posted. But, I'm a wimp and wanna steer clear of databases for this app. I calculate all the values manually in my code and it will be too much hassle to convert them to db tables! But thanks anyway! ;-)
 
hi Stretchwickster

No probs. I posted all the code as I think there may be some commands in there which are common to both types of charts - I think the technique will be the same. 'Fraid you'll just have to strip out the uninteresting bits.

I still think that if the scales are very different, stick the other 2 on the right hand side of the chart and adjust the axis accordingly to show optimum wotsit.

lou
 
Sorry Stretchwickster, the name of the event in TChart exactly is &quot;OnGetAxisLabel&quot;.

I faced the same problem with big/small scales. I fixed it by simply calculating nice limits and then applying a factor to the series values. This factor has to be taken care of in OnGetAxisLabel event, then it works nice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top