Logarithmic axis and negaive values

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
bairog
Newbie
Newbie
Posts: 72
Joined: Fri Jul 07, 2006 12:00 am
Location: Moscow, Russia
Contact:

Logarithmic axis and negaive values

Post by bairog » Tue Jul 14, 2009 9:17 am

I have a chart with two bar graphs (one is for positive values and enother is for negative):
Image

I have only values below 5 and above 1000, so I tried to use logarithmic axis. But negative values are not possible for that axis type.
So I ask for help: i there any way to draw log(X) graph for first bar graph and -log(X) for second one?
I mean that I want to have a chart like picture above but with logarithmic left axis.
Thank you.

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

Re: Logarithmic axis and negaive values

Post by Narcís » Tue Jul 14, 2009 9:32 am

Hi bairog,

No, logarithm doesn't support negative values:

http://en.wikipedia.org/wiki/Logarithm# ... lex_number

Complex logarithm scaling is not supported in TeeChart.

Having said that, the only solution I can think of is that you use 2 axes, one for the positive part and another one for the negative part. I recommend you to read tutorial 4 for further axis setting information. You could use an inverted logarithmic axis for the negative part, adding log labels to each point when populating them and add the "-" symbol using the GetAxisLabel event or already adding it with the points labels.

Hope this helps!
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

bairog
Newbie
Newbie
Posts: 72
Joined: Fri Jul 07, 2006 12:00 am
Location: Moscow, Russia
Contact:

Re: Logarithmic axis and negaive values

Post by bairog » Tue Jul 14, 2009 1:26 pm

Well, taking into account printing, scrolling and zooming i suppose that the easiest way is to do the following operations:
1) use a chart with non-logarithmic left axis
2) add log(x) instead of positive values that >= 10, -log(|X|) instead of negative values that <= -10, X/10 instead all others
3) recalculate left axis labels using GetAxisLabel event
4) replace series marks text with original values using GetSeriesMarks

I've implemented this and everything was ok except one thing. There are duplicate left axis marks (originaly they were 0, 1, 1, 2, 2, etc):
Image

How can I avoid duplicating left axis marks (I mean I want left axis marks to be: 0, 10, 100, 1000 etc.) automatically?
Thank you.

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

Re: Logarithmic axis and negaive values

Post by Narcís » Tue Jul 14, 2009 1:47 pm

Hi bairog,

The easiest solution I can think of is not drawing every second mark, setting it to an empty string, in the GetAxisLabel event.
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

bairog
Newbie
Newbie
Posts: 72
Joined: Fri Jul 07, 2006 12:00 am
Location: Moscow, Russia
Contact:

Re: Logarithmic axis and negaive values

Post by bairog » Wed Jul 15, 2009 5:33 am

Narcís wrote:The easiest solution I can think of is not drawing every second mark
The situation is more difficult than you suppose. Let me show you:

Image
In this situation I should remove all labels except second in each duplicate labels sequence (second label is correct one)

Image
In this situation I should remove all labels except third in each duplicate labels sequence (third label is correct one). Notice that last sequence (the sequence of -10000's) is shorter than all others.

Well in that case the algorithm of finding the correct label which will work in both situations to my mind is:

Code: Select all

int CorretLabelIndex = MinDuplicateLabelIndex + (MaxDuplicateLabelIndex - MinDuplicateLabelIndex) / 2
All other duplicate labels except correct one should be set to empty string (or not visible).

BUT:
1) I'm not sure that this algorithm will work correctly in all othes situations
2) Programming GetAxisLabel event to find all duplicate labels sequences, calculating correct label index and removing all others for each sequence in not short and trivial. I'm looking for the easiest way.
3) I supposed that such powerfull component like TeeChart has automatic mechanisms to remove duplicate labels.

BTW
Notice (if you didn't already) that I'm using "0" FormatValue for left axis labels (so they become 0, 10, 100, etc. after my manual recalculatting on step 3)


So what can you suggest me in such case?
Thank you.

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

Re: Logarithmic axis and negaive values

Post by Narcís » Wed Jul 15, 2009 1:38 pm

Hi bairog,

Could you please attach a simple example project we can run "as-is" so that we can reproduce the issue here and try to find the optimal solution?

An alternative could be using custom labels as in the All Features\Welcome!\Axes\Labels\Custom labels example in the features demo available at TeeChart's program group.

Thanks in advance.
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

bairog
Newbie
Newbie
Posts: 72
Joined: Fri Jul 07, 2006 12:00 am
Location: Moscow, Russia
Contact:

Re: Logarithmic axis and negaive values

Post by bairog » Thu Jul 16, 2009 12:37 pm

Narcís wrote:Could you please attach a simple example project we can run "as-is" so that we can reproduce the issue here and try to find the optimal solution?
Do you mean you need an example that can show both situations described above?
Ok, here it is.
Due to maximum file size limitations there are two arhives: program and teeChart.dll I was using.
Attachments
WindowsApplication3.7z
(79.06 KiB) Downloaded 555 times
TeeChart.7z
(510.91 KiB) Downloaded 623 times
Thank you.

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

Re: Logarithmic axis and negaive values

Post by Yeray » Fri Jul 17, 2009 10:43 am

Hi bairog,

Could you please take a look at this proposal?

Code: Select all

        public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }

        Bar bar1, bar2;

        private void InitializeChart()
        {
            chartController1.Chart = tChart1;

            tChart1.Aspect.View3D = false;
            tChart1.Legend.Visible = false;

            bar1 = new Bar(tChart1.Chart);
            bar2 = new Bar(tChart1.Chart);

            bar1.MultiBar = MultiBars.Stacked;

            tChart1.Axes.Left.MinorTicks.Visible = false;

            tChart1.Axes.Left.Labels.Items.Clear();
            tChart1.Axes.Left.Labels.Items.Add(0);

            bar1.Add(1 / 10.0);
            bar2.Add(-1 / 10.0);
            tChart1.Axes.Left.Labels.Items.Add(1 / 10.0, "1");
            tChart1.Axes.Left.Labels.Items.Add(-1 / 10.0, "-1");

            AddLog(bar2, -1000);
            AddLog(bar2, -10);
            AddLog(bar2, -10000);

            bar1.GetSeriesMark += new Series.GetSeriesMarkEventHandler(bar1_GetSeriesMark);
            bar2.GetSeriesMark += new Series.GetSeriesMarkEventHandler(bar1_GetSeriesMark);
        }

        private void AddLog(Bar bar, double p)
        {
            if (p > 0)
            {
                bar.Add(Math.Log10(p));
                tChart1.Axes.Left.Labels.Items.Add(bar.YValues.Value[bar.Count - 1], p.ToString());
            }
            else
            {
                bar.Add(-Math.Log10(Math.Abs(p)));
                tChart1.Axes.Left.Labels.Items.Add(bar.YValues.Value[bar.Count - 1], p.ToString());
            }
        }

        private void bar1_GetSeriesMark(Steema.TeeChart.Styles.Series series, Steema.TeeChart.Styles.GetSeriesMarkEventArgs e)
        {
            if (series == bar1)
                e.MarkText = "1";
            else
                if (e.ValueIndex == 0)
                    e.MarkText = "-1";
                else
                    if (e.ValueIndex == 1)
                        e.MarkText = "-1000";
                    else
                        if (e.ValueIndex == 2)
                            e.MarkText = "-10";
                        else
                            e.MarkText = "-10000";
        }
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