我在用 ms chart ,用于绘制 K线图。。目前,我遇到问题了,本来,数据里面,肯定是x轴和y轴一一对应的,比如:x         y
1        1.1
2        1.2
2.1      1.3
3         2
...我想要的结果是:x轴的数据固定显示,比如, 最大值是 3500,间隔 500 显示一条主刻度线,那么 X轴应该显示:500,1000,1500,2000,2500,3000,3500。其中,500-1000的范围之内,可能没有数据,但是,也要显示为空。。也就是,不管有木有数据,X轴都是有 7个 点,整个图表,固定分为7列 。然后,都在这其中的范围之内描点。我查了很多资料,都没搞定,请问,你们有木有人 搞过 ms chart控件,给我个思路,或者示例 。。不要推荐我 使用 其他控件,我就是 要用 ms chart 谢谢了。。

解决方案 »

  1.   

    right,没数据,Y 就相当于 为 0 
      

  2.   

    if(没数据)
    {
            Chart1.Series[0].Points.AddY(0);
            //设置X轴
             Chart1.ChartAreas[0].AxisX.Interval = 500;  
            Chart1.ChartAreas[0].AxisX.Maximum = 3500;
            Chart1.ChartAreas[0].AxisX.Minimum = 500;
            //设置y轴
             Chart1.ChartAreas[0].AxisY.Interval = 20;
            Chart1.ChartAreas[0].AxisY.Maximum = 100;
    }
      

  3.   

    但是,好像不行哦 
    this.chart1.ChartAreas[0].AxisX.Interval = 500;
                    this.chart1.ChartAreas[0].AxisX.Maximum = 3500;
                    this.chart1.ChartAreas[0].AxisX.Minimum = 0;
                    this.chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount; 
      

  4.   

    如果是这样,你代码哪里有错了!!
    设置一下series的XValueType的数据类型!!
      

  5.   


     #region 生成K线图
            private void button4_Click(object sender, EventArgs e)
            {
                if (dt.Columns.Count > 1)
                {
                    string bname = dt.Columns[0].ColumnName.Trim();//X轴数据
                    string cname = dt.Columns[1].ColumnName.Trim();//Y轴数据                DataTable dt1 = CreateTable();
                    this.chart1.DataSource = dt;//dt 是全局变量,datatable类型
                    int count = 350;                if (this.chart1.Series.Count > 0)
                    {
                        this.chart1.Series.Remove(this.chart1.Series[cname]);
                    }
                    this.chart1.Series.Add(cname);                if (this.chart1.ChartAreas.Count > 0)
                    {
                        this.chart1.ChartAreas.Remove(this.chart1.ChartAreas["ChartArea1"]);
                    }
                    this.chart1.ChartAreas.Add("ChartArea1");//增加图表区                if (dt.Columns.Count > 2)
                    {
                        string xname = dt.Columns[0].ColumnName.Trim();//X轴数据
                        string yname = dt.Columns[2].ColumnName.Trim();//Y轴数据                    this.chart1.DataSource = dt;                    if (this.chart1.Series.Count > 0)
                        {
                            this.chart1.Series.Remove(this.chart1.Series[yname]);
                        }
                        this.chart1.Series.Add(yname);                    if (this.chart1.ChartAreas.Count > 1)
                        {
                            this.chart1.ChartAreas.Remove(this.chart1.ChartAreas["ChartArea2"]);
                        }
                        this.chart1.ChartAreas.Add("ChartArea2");//增加图表区
                        this.chart1.Series[yname].ChartArea = "ChartArea2";
                        this.chart1.ChartAreas[1].AxisX.Title = xname;
                        this.chart1.ChartAreas[1].AxisY.Title = yname;                    this.chart1.Series[yname].ChartType = SeriesChartType.Spline;
                        this.chart1.Series[yname].XValueMember = xname;
                        this.chart1.Series[yname].YValueMembers = yname;
                        //chart1.Series["Series1"].Points.DataBindXY(dt.DefaultView, "x", dt.DefaultView, "y");
                    }                this.chart1.Series[cname].ChartArea = "ChartArea1";
                    this.chart1.ChartAreas[0].AxisX.Title = bname;
                    this.chart1.ChartAreas[0].AxisY.Title = cname;
                    //this.chart1.ChartAreas[0].BackColor = Color.FromArgb(209, 237, 254);                // 坐标轴设置
                    this.chart1.ChartAreas[0].AxisY.IsMarginVisible = false;
                    // 坐标轴刻度线不延长出来设置
                    this.chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
                    this.chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
                    //X 次要辅助线设置
                    this.chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
                    //X 次要辅助线间距
                    //this.chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 500;
                    //X 次要辅助线颜色
                    this.chart1.ChartAreas[0].AxisX.MinorGrid.LineColor = Color.LightGray;
                    //Y 次要辅助线设置
                    this.chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
                    //Y 次要辅助线间距
                    this.chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 5;
                    //Y 次要辅助线颜色
                    this.chart1.ChartAreas[0].AxisY.MinorGrid.LineColor = Color.LightGray;
                    //X 主要辅助线设置
                    this.chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
                    //X 主要辅助线间距
                    //this.chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 500;
                    //X 主要辅助线颜色
                    this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
                    //Y 主要辅助线设置
                    this.chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
                    //Y 主要辅助线间距
                    this.chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 5;
                    //Y 主要辅助线颜色
                    this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Black;
                    //X 轴坐标最大最小值
                    this.chart1.ChartAreas[0].AxisX.Interval = 500;
                    this.chart1.ChartAreas[0].AxisX.Maximum = 3500;
                    this.chart1.ChartAreas[0].AxisX.Minimum = 0;
                    //this.chart1.ChartAreas[0].AxisX.MaximumAutoSize = 100;
                    this.chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount; 
                    this.chart1.ChartAreas[0].AxisY.Interval = 5;
                    //图例位置、字体设置;坐标轴位置设定
                    //this.chart1.Legends[0].Position = new ElementPosition(10, 10, 88, 7);
                    //this.chart1.Legends[0].Font = new Font("宋体", 9);
                    //this.chart1.ChartAreas[0].InnerPlotPosition = new ElementPosition(6, 5, 90, 82);                this.chart1.Series[cname].ChartType = SeriesChartType.Spline;
                    this.chart1.Series[cname].XValueMember = bname;
                    this.chart1.Series[cname].YValueMembers = cname;
                    this.chart1.Series[cname].XValueType = ChartValueType.Double;
                    this.chart1.Series[cname].YValueType = ChartValueType.Double;                //chart1.Series[cname].Points.DataBindXY(dt1.DefaultView, "x", dt.DefaultView, cname);
                    this.chart1.DataBind();
                }
            }
            #endregion
      

  6.   

    用Chart1.Series[0].Points.DataBindXY()绑定数据试试
      

  7.   

    做了一下,特地取了一个1014.0288的数据,较为分开,显示在1000附近!
    double[] x = { 500, 605.9718, 611.95203, 617.98438, 624.00958, 1014.0288 };
    double[] y = { 19.7936, 19.7376, 19.834349, 19.755239, 18.81985, 21.722811 };
    Chart1.Series[0].Points.DataBindXY(x, y);
    Chart1.ChartAreas[0].AxisX.Interval  =  500;
    Chart1.ChartAreas[0].AxisX.Maximum = 3500;
    Chart1.ChartAreas[0].AxisX.Minimum = 0;
      

  8.   

    我发觉,我把数据放到 double 数组里面,效果就正常了。 原来那种绑定,就不正常。。太奇怪了
      

  9.   

    如果直接读取数据库的表,字段类型是Decmal,
    是完全没问题的!!楼主的CreateTable()不知是调用的是什么!
    将DataTable转成DataView试试!
    DataView myView = new DataView(dt1);
    Chart1.Series[0].Points.DataBindXY(myView, "x轴字段", myView, "yx轴字段");//绑定