aihenry2980
aihenry2980

Reputation: 329

How can i modify the background of the chart

  1. How can I remove the "-----Series1" text (top right, near button1)
  2. How can I make the blue stepline thinker, and remove the background lines?
  3. How can I draw color to the background, such as the area from y=0 to y=1 colored as grey.
  4. How can I add mousewheel event, so that i can use Ctrl+mousewheel to zoom in and out the chart?

Thank you!

enter image description here

Upvotes: 0

Views: 2192

Answers (1)

endofzero
endofzero

Reputation: 1818

Below I have provided some example code to answer your questions. My answers to 3 and 4 are basic examples, and you will have to calculate the correct values for painting and zooming in your application.

    private void button1_Click(object sender, EventArgs e)
    {
        setupChart();
    }

    private void setupChart()
    {
        // 1.
        foreach (System.Windows.Forms.DataVisualization.Charting.Legend legend in chart1.Legends)
            legend.Enabled = false;

        // 2.
        chart1.Series[0].BorderWidth = 5;
        chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
        chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;

        // 3.
        chart1.Paint += new PaintEventHandler(chart1_Paint);

        // 4.
        chart1.MouseWheel += new MouseEventHandler(chart1_MouseWheel);
        //chart must have focus for MouseWheel event to fire
        chart1.Focus();
    }

    private void chart1_Paint(object sender, PaintEventArgs e)
    {
        //basic example of painting

        //determine size of area to paint
        Size areaSize = new Size(50, 50);

        //determine location to paint
        Point point = new Point(100, 450);

        e.Graphics.FillRectangle(new SolidBrush(Color.Gray),
            point.X, point.Y, areaSize.Width, areaSize.Height);
    }

    private void chart1_MouseWheel(object sender, MouseEventArgs e)
    {
        //basic example of zooming in and out
        if (e.Delta > 0)
        {
            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(
                chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum / 2,
                chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum / 2);
            chart1.ChartAreas[0].AxisY.ScaleView.Zoom(
                chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum / 2,
                chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum / 2);
        }
        else
        {
            if (chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum <
                chart1.ChartAreas[0].AxisX.Maximum ||
                chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum >
                chart1.ChartAreas[0].AxisX.Minimum)
            {
                chart1.ChartAreas[0].AxisX.ScaleView.Zoom(
                    chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum * 2,
                    chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum * 2);
                chart1.ChartAreas[0].AxisY.ScaleView.Zoom(
                    chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum * 2,
                    chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum * 2);
            }
            else
            {
                chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
                chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
            }
        }
    }

Upvotes: 2

Related Questions