示例#1
0
 public DataChart(string chartId)
 {
     InitialMsChart();
     xChart = new XChart(chartId);
     SetChartData(xChart);
 }
示例#2
0
        private void SetChartData_(XChart xChart)
        {
            Title title = msChart.Titles.Add("aaaa");

            title.Text = "测试图表";

            msChart.Series.Clear();

            Legend lg = msChart.Legends.FindByName("Default");

            if (lg == null)
            {
                lg = new Legend("Default");
                msChart.Legends.Add(lg);
            }

            try
            {
                lg.Docking = Docking.Top;
            }
            catch { }

            try
            {
                lg.Alignment = StringAlignment.Center;
            }
            catch { }

            lg.LegendStyle = LegendStyle.Column;

            lg.LegendStyle = LegendStyle.Row;



            //lg.Alignment = StringAlignment.Near;
            //lg.Position.X = 0;
            //lg.Position.Y = 0;
            lg.Title     = "xxxxx";
            lg.BackColor = Color.Transparent;

            if (msChart.Titles.Count < 1)
            {
                msChart.Titles.Add("Chart");
            }

            msChart.Width  = 300;
            msChart.Height = 300;

            System.Web.UI.DataVisualization.Charting.ChartArea area = msChart.ChartAreas.Add("Default");

            //            System.Web.UI.DataVisualization.Charting.ChartArea area = chart.ChartAreas[0];



            area.Area3DStyle.Enable3D = true;
            //  area.AxisY.IsLogarithmic = true;
            //  area.AxisX.IsLogarithmic = true;

            //            area.AxisX.Interval = 0;
            area.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
            area.AxisX.IsMarginVisible  = false;
            area.BackColor = Color.Pink;
            // area.AxisX.

            //            area.AxisX.Interval = 1;
            Series a = msChart.Series.Add("a");

            a.Legend    = "Default";
            a.ChartArea = "Default";
            a.Points.AddXY(1, 23);

            a.Points.AddXY(2, 84);
            a.Points.AddXY(3, 45);
            a.Points.AddXY(4, 245);
            a.Points.AddXY(5, 34);
            a.Points.AddXY(6, 34);
            a.Points.AddXY(7, 33);
            //            a.PostBackValue = "#AXISLABEL";
            a.PostBackValue = "xxxxxxx";
            a.ToolTip       = "#LEGENDTEXT \n" +
                              "X轴:#VALX\n" +
                              "Y轴:#VAL";
        }
示例#3
0
        private void SetChartData(XChart xChart)
        {
            ChartSchema schema = xChart.Schema;

            DataSource table = null;

            if (!String.IsNullOrEmpty(schema.DataSource))
            {
                table = new DataSource(schema.DataSource);
                //    table.RefreshData();
                //    table.PageSize = 100;
            }

            //    Dictionary<string, ListData<string>> keyValues = table.FieldValues;
            //    Dictionary<string, string> captions = table.FieldCaptions;

            //    msChart.ChartAreas.Clear();
            //   msChart.ChartAreas[0].
            //  msChart.ChartAreas[0].Area3DStyle=ChartArea3DStyle.
            msChart.Series.Clear();

            Legend lg = msChart.Legends.FindByName("Default");

            if (lg == null)
            {
                lg = new Legend("Default");
                msChart.Legends.Add(lg);
            }

            try
            {
                lg.Docking = (Docking)int.Parse(schema.LegenPos);
            }
            catch { }

            try
            {
                lg.Alignment = (StringAlignment)int.Parse(schema.LegenAlign);
            }
            catch { }

            lg.LegendStyle = LegendStyle.Column;
            if (schema.LegenH)
            {
                lg.LegendStyle = LegendStyle.Row;
            }



            //lg.Alignment = StringAlignment.Near;
            //lg.Position.X = 0;
            //lg.Position.Y = 0;


            if (msChart.Titles.Count < 1)
            {
                msChart.Titles.Add("Chart");
            }
            Title title = msChart.Titles[0];

            title.Text = schema.Title;
            try
            {
                title.Alignment = (ContentAlignment)int.Parse(schema.TitlePos);
            }
            catch
            {
                title.Alignment = ContentAlignment.TopCenter;
            }

            //            if (schema.Width > 200)
            msChart.Width = schema.Width;
            //   if (schema.Width > 710)
            //       msChart.Width = 710;

            if (schema.Height > 100)
            {
                msChart.Height = schema.Height;
            }


            //            System.Web.UI.DataVisualization.Charting.ChartArea area = msChart.ChartAreas[0];
            msChart.ChartAreas.Clear();
            System.Web.UI.DataVisualization.Charting.ChartArea area = msChart.ChartAreas.Add("Default");

            if (!string.IsNullOrEmpty(schema.BackColor))
            {
                msChart.BackColor = ToColor(schema.BackColor);
            }

            if (!string.IsNullOrEmpty(xChart.AreaBackColor))
            {
                area.BackColor = ToColor(xChart.AreaBackColor);
            }


            area.Area3DStyle.Enable3D = schema.Is3D;
            //  area.AxisY.IsLogarithmic = true;
            //  area.AxisX.IsLogarithmic = true;

            //            area.AxisX.Interval = 0;
            area.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
            area.AxisX.IsMarginVisible  = false;
            // area.AxisX.

            area.AxisX.Interval = schema.XInterval;

            try
            {
                area.AxisX.IntervalType = (DateTimeIntervalType)int.Parse(schema.XIntervalUnit);
            }
            catch
            {
                area.AxisX.IntervalType = DateTimeIntervalType.Auto;
            }

            area.AxisX.IsLabelAutoFit    = true;
            area.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont | LabelAutoFitStyles.IncreaseFont |
                                           LabelAutoFitStyles.LabelsAngleStep90 | LabelAutoFitStyles.LabelsAngleStep45 |
                                           LabelAutoFitStyles.LabelsAngleStep30;

            area.AxisX.LabelStyle.Enabled = true;
            area.AxisX.LabelStyle.Format  = schema.XLableFormat;

            //  lg.



            //            area.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;

            //            area.AxisX.IntervalOffsetType =DateTimeIntervalType.Minutes;

            if (schema.AreaList[0].MinY == float.NaN || schema.AreaList[0].MaxY == float.NaN || schema.AreaList[0].MaxY == 0 || schema.AreaList[0].MinY == 0)
            {
                area.AxisY.Minimum = Double.NaN;
                area.AxisY.Maximum = Double.NaN;
            }
            else
            {
                // area.AxisY.IsLogarithmic = true;
                area.AxisY.Minimum = schema.AreaList[0].MinY;
                area.AxisY.Maximum = schema.AreaList[0].MaxY;
            }


            string[] xValues = null;
            if (table != null && !String.IsNullOrEmpty(schema.XField))
            {
                xValues = table.GetFieldData(schema.XField);
            }


            area.AxisX.MajorGrid.Enabled   = schema.ShowGridHLine;
            area.AxisX.MajorGrid.LineColor = ToColor(schema.GridHLineColor);
            area.AxisX.MajorGrid.LineWidth = schema.GridHLineWidth;

            area.AxisY.MajorGrid.Enabled   = schema.ShowGridVLine;
            area.AxisY.MajorGrid.LineColor = ToColor(schema.GridVLineColor);
            area.AxisY.MajorGrid.LineWidth = schema.GridVLineWidth;

            int mutYOffset = MutiYOffSetUnit;

            if (xChart.XYReverse)
            {
                ShowReverseSeries(table, area);
            }
            else
            {
                foreach (SeriesSchema se in schema.SeriesList)
                {
                    Series series = msChart.Series.Add(se.Id);
                    series.ChartType   = (SeriesChartType)se.ChartType;
                    series.Color       = ToColor(se.Color);
                    series.MarkerStyle = (System.Web.UI.DataVisualization.Charting.MarkerStyle)se.MarkerStyle;
                    //  series.AxisLabel=
                    series.ChartArea             = area.Name;
                    series.BorderWidth           = se.LineWidth;
                    series.Legend                = "Default";
                    series.YAxisType             = AxisType.Primary;
                    area.AxisY.IsStartedFromZero = se.IsFromY0;
                    // series.
                    if (se.UsingY2)
                    {
                        series.YAxisType                 = AxisType.Secondary;
                        area.AxisY2.LineColor            = series.Color;
                        area.AxisY2.LabelStyle.ForeColor = series.Color;
                        area.AxisY2.IsStartedFromZero    = se.IsFromY0;
                    }
                    if (table != null)
                    {
                        try
                        {
                            series.LegendText = table.FieldCaptions[se.Id];
                        }
                        catch
                        {
                            series.LegendText = se.Id;
                        }


                        string[] values     = new string[0];
                        string[] valuesUp   = new string[0];
                        string[] valuesDown = new string[0];

                        int count = 0;
                        try { values = table.GetFieldData(se.Id); }
                        catch { }
                        if (values != null)
                        {
                            count = values.Length;
                        }

                        try { valuesUp = table.GetFieldData(se.UpSeries); }
                        catch { }
                        if (valuesUp != null && valuesUp.Length > count)
                        {
                            count = valuesUp.Length;
                        }

                        try { valuesDown = table.GetFieldData(se.DownSeries); }
                        catch { }
                        if (valuesDown != null && valuesDown.Length > count)
                        {
                            count = valuesDown.Length;
                        }



                        for (int i = 0; i < count; i++)
                        {
                            double v = 0;
                            try
                            {
                                v = double.Parse(values[i]);
                            }
                            catch { }

                            double vUp = 0;
                            try
                            {
                                vUp = double.Parse(valuesUp[i]);
                            }
                            catch { }

                            double vDown = 0;
                            try
                            {
                                vDown = double.Parse(valuesDown[i]);
                            }
                            catch { }

                            object x = i;
                            try { x = xValues[i]; }
                            catch { }

                            if (area.AxisX.IntervalType != DateTimeIntervalType.Auto &&
                                area.AxisX.IntervalType != DateTimeIntervalType.NotSet &&
                                area.AxisX.IntervalType != DateTimeIntervalType.Number)
                            {
                                try { x = DateTime.Parse(xValues[i]); }
                                catch { }
                            }
                            else
                            {
                                try { x = Double.Parse(xValues[i]); }
                                catch { }
                            }



                            switch (series.ChartType)
                            {
                            case SeriesChartType.SplineRange:
                            case SeriesChartType.Range:
                            case SeriesChartType.RangeColumn:
                            case SeriesChartType.RangeBar:
                                //  series.Points.AddY(vUp, vDown);
                                series.Points.AddXY(x, vUp, vDown);
                                break;

                            default:
                                series.Points.AddXY(x, v);
                                break;
                            }

                            //     if (xValues != null)
                            //         series.Points[i].AxisLabel = xValues[i];
                        }

                        //       Legend lg = new Legend(se.Id);
                        // lg.s
                        //       msChart.Legends.Add(lg);
                        //     lg.Title = table.FieldCaptions[se.Id];
                        //msChart.Legends[se.Id].Title = table.FieldCaptions[se.Id];
                    }
                    if (se.CreateAxisY && !se.UsingY2)
                    {
                        this.CreateYAxis(msChart, area, series, mutYOffset, MutiYOffSetUnit);
                        mutYOffset += MutiYOffSetUnit;
                    }

                    series.PostBackValue = "#AXISLABEL";
                    series.ToolTip       = "#LEGENDTEXT \n" +
                                           "X轴:#VALX\n" +
                                           "Y轴:#VAL";
                }
            }
            //msChart.Style=

            //    foreach (BI.Schema.ChartArea areaSch in schema.AreaList)
            //    {
            //        System.Web.UI.DataVisualization.Charting.ChartArea chartArea = msChart.ChartAreas.Add(areaSch.Id);
            //    }
        }//Method