Search code examples
c#winformsteechart

How to remove some part of start, end of TeeChart


I am working with windows form application in c#. I have installed licensed version of TeeChart for.net v3. I am trying to remove some unwanted portion of TeeChart.
Thanks to stackoverflow website users, they helped me to create a mouse click popup on TeeChart. Now i want to implement some functionality for that popup clicks.
using the below code i am able to create mouse right click popup.

double m_dblTempVolFromTo = 0;
double dtFromTo = 0;
private void mainTChart_MouseUp(object sender, MouseEventArgs e)
{
    if (!checkBoxIsTime.Checked && e.Button == MouseButtons.Right)
    {
        m_dblTempVolFromTo = mainTChart.Series[0].XScreenToValue(e.X);
        mainTChart.ContextMenu = new ContextMenu();
        mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + m_dblTempVolFromTo + " cc"));
        mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + m_dblTempVolFromTo + " cc"));
        mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
        mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);

    }
    else if (checkBoxIsTime.Checked && e.Button == MouseButtons.Right)
    {
        DateTime dt;
        dtFromTo = mainTChart.Series[0].XScreenToValue(e.X);
        DateTime.TryParse(dtFromTo.ToString(), out dt);
        mainTChart.ContextMenu = new ContextMenu();
        mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + dt.TimeOfDay.ToString() ));
        mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + dt.TimeOfDay.ToString()));
        mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
        mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
    }

}

The above code is creating popup like as shown below. Image
I am trying to implement functionality for "For 7.6 cc" and "To 7.6 cc". when i click "To 7.6 cc" then the chart should remove from "0 to 7.6" scale and remaining part should be there. As well the same apply for "From 145 cc" click, it has to remove the chart from "145 to 150(end of scale)".
click on "To" is using to remove starting portion of chart and click on "From" is using to remove end portion of chart.
I have tried like this but i am not able to get what i want.

void From_Click(object sender, EventArgs e)
{
    if (!checkBoxIsTime.Checked)
    {
        var destBitmap = mainTChart.Export.Image.Bitmap.Clone(new Rect(0, 0, 100, 200), sourceBitmap.PixelFormat);
    }
}

void To_Click(object sender, EventArgs e)
{

} 

even i have tried with this code also

void mainTChart_GetLegendRect(object sender, mainTChart.GetLegendRectEventArgs e)
{
    Rectangle cropRect = e.Rectangle;
    Bitmap legendImg = new Bitmap(cropRect.Width, cropRect.Height);

    using (Graphics g = Graphics.FromImage(legendImg))
    {
      g.DrawImage(chartBmp, new Rectangle(0, 0, mainTChart.Width, mainTChart.Height),
                 cropRect,
                 GraphicsUnit.Pixel);
    }
} 

nothing is working for me. Can any one help me with this task.

Thanks in advance.

Edited when i have X-axis as time then i am not able to get display time on mouse click. The code I have tried as shown below

 DateTime dt;
        dtFromTo = mainTChart.Series[0].XScreenToValue(e.X);
        DateTime.TryParse(dtFromTo.ToString(), out dt);
        mainTChart.ContextMenu = new ContextMenu();
        mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + dt.TimeOfDay.ToString() ));
        mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + dt.TimeOfDay.ToString()));

I am getting like this enter image description here I am getting as shown in the above image but want to display the equivalent time on mouse right click. I am getting some value to this variable "dtFromTo" like 41322.9876587965" but i am not able to convert that value into time. please help me.


Solution

  • Known the values you want to "cut" From/To, you can just set the Bottom axis Minimum and Maximum properties.

    when i click "To 7.6 cc" then the chart should remove from "0 to 7.6" scale and remaining part should be there

    This would be:

    mainTChart.Axes.Bottom.AutomaticMinimum = false;
    mainTChart.Axes.Bottom.Minimum = 7.6;
    

    As well the same apply for "From 145 cc" click, it has to remove the chart from "145 to 150(end of scale)".

    This would be done setting the Maximum:

    mainTChart.Axes.Bottom.AutomaticMaximum = false;
    mainTChart.Axes.Bottom.Maximum = 145;
    

    So I think this should to the trick:

        void From_Click(object sender, EventArgs e)
        {
            mainTChart.Axes.Bottom.AutomaticMaximum = false;
            mainTChart.Axes.Bottom.Maximum = m_dblTempVolFromTo;
        }
    
        void To_Click(object sender, EventArgs e)
        {
            mainTChart.Axes.Bottom.AutomaticMinimum = false;
            mainTChart.Axes.Bottom.Minimum = m_dblTempVolFromTo;
        } 
    

    I'd also suggest you to create the ContextMenu in mainTChart_MouseDown event insted of mainTChart_MouseUp because creating it at the mainTChart_MouseUp event is too late, the ContextMenu shown won't show the updated version.

    Edit:

    For DateTime XValues, the wrong line is this one:

    DateTime.TryParse(dtFromTo.ToString(), out dt);
    

    Here it is the full code that seems to work fine for me here:

        private void InitializeChart()
        {
            mainTChart.Aspect.View3D = false;
    
            Line line1 = new Line(mainTChart.Chart);
            line1.XValues.DateTime = true;
            line1.FillSampleValues();
            mainTChart.Axes.Bottom.Labels.DateTimeFormat = "hh:mm";
    
            mainTChart.MouseDown += new MouseEventHandler(mainTChart_MouseDown);
        }
    
        double m_dblTempVolFromTo = 0;
        double dtFromTo = 0;
    
        void mainTChart_MouseDown(object sender, MouseEventArgs e)
        {
            if (!mainTChart.Axes.Bottom.IsDateTime && e.Button == MouseButtons.Right)
            {
                m_dblTempVolFromTo = mainTChart[0].XScreenToValue(e.X);
                mainTChart.ContextMenu = new ContextMenu();
                mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + m_dblTempVolFromTo + " cc"));
                mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + m_dblTempVolFromTo + " cc"));
                mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
                mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
    
            }
            else if (e.Button == MouseButtons.Right)
            {
                dtFromTo = mainTChart[0].XScreenToValue(e.X);
                String stFromTo = mainTChart.Axes.Bottom.Labels.LabelValue(dtFromTo);
                mainTChart.ContextMenu = new ContextMenu();
                mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + stFromTo));
                mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + stFromTo));
                mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
                mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
            }
        }
    
        void From_Click(object sender, EventArgs e)
        {
            mainTChart.Axes.Bottom.AutomaticMaximum = false;
            if (!mainTChart.Axes.Bottom.IsDateTime)
                mainTChart.Axes.Bottom.Maximum = m_dblTempVolFromTo;
            else
                mainTChart.Axes.Bottom.Maximum = dtFromTo;
        }
    
        void To_Click(object sender, EventArgs e)
        {
            mainTChart.Axes.Bottom.AutomaticMinimum = false;
            if (!mainTChart.Axes.Bottom.IsDateTime)
                mainTChart.Axes.Bottom.Minimum = m_dblTempVolFromTo;
            else
                mainTChart.Axes.Bottom.Minimum = dtFromTo;
        }