none
chart中的三條series...如何新增使用不同的單位Y軸 RRS feed

  • 問題

  • 請問...不知道我新增Y軸的方式哪出了問題...
    (3條曲線想用3種單位個別顯示)都只出現預設Y...並沒產生 Series2 跟 Series3的Y軸,
    造成每次數據進來 圖形都無法在chartarea中間顯示
    Y軸的數值範圍會跳動的問題 ...

    private void m_Client_Load(object sender, EventArgs e)
            {
                
                // Set custom chart area position
                Chart1.ChartAreas["Default"].Position = new ElementPosition(15, 5, 80, 85);
                Chart1.ChartAreas["Default"].InnerPlotPosition = new ElementPosition(8, 0, 95, 95);
    
                // Create extra Y axis for second and third series
                //增加第二第三個Y軸,格式--> CreateYAxis(chart,chartarea,serial,x軸的偏移量,標籤的大小)
                CreateYAxis(Chart1, Chart1.ChartAreas["Default"], Chart1.Series["Series2"], 10, 8);
                CreateYAxis(Chart1, Chart1.ChartAreas["Default"], Chart1.Series["Series3"], 13, 8);
                
    //預設圖形            
                for (int pointIndex = 0; pointIndex < 300; pointIndex++)
                {
                    Chart1.Series["Series1"].Points.AddXY(pointIndex, Math.Sin(pointIndex) * 0.1);
                    Chart1.Series["Series2"].Points.AddXY(pointIndex, pointIndex);
                    Chart1.Series["Series3"].Points.AddXY(pointIndex, pointIndex - 10);
                    
                }                       
    
                
            }
    
    public void CreateYAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
    		{
                // Create new chart area for original series
                ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
                areaSeries.BackColor = Color.Transparent;
                areaSeries.BorderColor = Color.Transparent;
                areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
                areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
                areaSeries.AxisX.MajorGrid.Enabled = false;
                areaSeries.AxisX.MajorTickMark.Enabled = false;
                areaSeries.AxisX.LabelStyle.Enabled = false;
                areaSeries.AxisY.MajorGrid.Enabled = false;
                areaSeries.AxisY.MajorTickMark.Enabled = false;
                areaSeries.AxisY.LabelStyle.Enabled = false;
                areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
                
    
                series.ChartArea = areaSeries.Name;
    
                // Create new chart area for axis
                ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
                areaAxis.BackColor = Color.Transparent;
                areaAxis.BorderColor = Color.Transparent;
                areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
                areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
    
                // Create a copy of specified series
                Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
                seriesCopy.ChartType = series.ChartType;
                foreach (DataPoint point in series.Points)
                {
                    seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
                }
    
                // Hide copied series
                seriesCopy.IsVisibleInLegend = false;
                seriesCopy.Color = Color.Transparent;
                seriesCopy.BorderColor = Color.Transparent;
                seriesCopy.ChartArea = areaAxis.Name;
    
                // Disable drid lines & tickmarks
                areaAxis.AxisX.LineWidth = 0;
                areaAxis.AxisX.MajorGrid.Enabled = false;
                areaAxis.AxisX.MajorTickMark.Enabled = false;
                areaAxis.AxisX.LabelStyle.Enabled = false;
                areaAxis.AxisY.MajorGrid.Enabled = false;
                areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
                areaAxis.AxisY.LabelStyle.Font = area.AxisY.LabelStyle.Font;
    
                // Adjust area position
                areaAxis.Position.X -= axisOffset;
                areaAxis.InnerPlotPosition.X += labelsSize;
    
                /*
                //在新增Y軸時更改Y軸顏色(同series顏色)
                areaAxis.AxisY.LineColor = Chart1.Series[series.Name].Color;
                areaAxis.AxisY.LabelStyle.ForeColor = Chart1.Series[series.Name].Color;
                
                
                //範圍選取
                Chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
                Chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
                areaAxis.CursorX.IsUserEnabled = true;
                areaAxis.CursorX.IsUserSelectionEnabled = true;
    
                //捲軸設定
                Chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
                areaAxis.AxisX.ScaleView.Zoomable = true;
    
                /* 放大縮小
                Chart1.CursorX.IsUserEnabled = true;
                Chart1.CursorX.IsUserSelectionEnabled = true;
                Chart1.CursorX.SelectionColor = System.Drawing.SystemColors.Highlight;
                Chart1.CursorY.IsUserEnabled = true;
                Chart1.CursorY.IsUserSelectionEnabled = true;
                Chart1.CursorY.SelectionColor = System.Drawing.SystemColors.Highlight;
                */
    
    	}


    2016年4月19日 上午 10:50

解答

所有回覆