zedgraph 折线图与柱形图,X轴文字竖向显示

 //柱状图创建函数

public void creatZZT(ZedGraphControl zgc, Dictionary<int, int> dic)
        {
         
            const double offset = 10;
            // 为每个点加标注
            GraphPane myPane = zgc.GraphPane;
            // Set the GraphPane title
            myPane.Title.Text = "像元统计图";
            myPane.Title.FontSpec.IsItalic = true;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "Times New Roman";

            myPane.XAxis.Title.Text = "像元值";
            myPane.YAxis.Title.Text = "像元数量";
            PointPairList list1 = new PointPairList();

            string[] labels = new string[dic.Count];
            for (int i = 0; i < dic.Count; i++)
            {
                list1.Add(i, dic[i]); //添加一组数据
                
            }
            labels[0] = "0-1";
            labels[1] = "1-5";
            labels[2] = ">5";
            double max = 0;
            foreach (double v in dic.Values) {
                if (v > max) {
                    max = v;

                }
            }
           myPane.AddBar("像元数", list1, Color.Black).Bar.Fill = new Fill(Color.Red, Color.White, Color.Red, 0f);
            for (int i = 0; i < labels.Length; i++)
            {
                // Get the pointpair
                //PointPair pt = list1[i].Points[i];
                // Create a text label from the Y data value
                TextObj text = new TextObj(dic[i].ToString(), i+1, dic[i] + max*1.0/20, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Red;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }

            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);


            myPane.XAxis.Scale.TextLabels = labels; //X轴文本取值

            myPane.XAxis.Type = AxisType.Text;   //X轴类型


            zgc.AxisChange();

            Refresh();
        }

//折线图创建函数

 public void creatpan(ZedGraphControl zgc, Dictionary<string, double> dic1, Dictionary<string, double> dic2, Dictionary<string, double> dic3)
        {
            const double offset = 10;
            // 为每个点加标注
            GraphPane myPane = zgc.GraphPane;
            // Set the GraphPane title
            myPane.Title.Text = "分区像元统计图";
            myPane.Title.FontSpec.IsItalic = true;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "Times New Roman";

            myPane.XAxis.Title.Text = "区县名称";
            myPane.YAxis.Title.Text = "像元值";
            PointPairList list1 = new PointPairList();
            PointPairList list2 = new PointPairList();
            PointPairList list3 = new PointPairList();
            string[] labels = new string[dic1.Count];
            for (int i = 0; i < dic1.Count; i++)
            {
                list1.Add(i+1, Convert.ToDouble(dic1[returnDicKey(dic1,i)])); //添加一组数据
                list2.Add(i+1, Convert.ToDouble(dic2[returnDicKey(dic2, i)])); //添加一组数据
                list3.Add(i+1, Convert.ToDouble(dic3[returnDicKey(dic3, i)])); //添加一组数据

            }
            int count = 0;
            foreach(string ss in dic1.Keys)
            {
                labels[count] = ss;
                count++;


            }
           LineItem curve= myPane.AddCurve("最小值", list1, Color.Black, SymbolType.Star);
           LineItem curve2= myPane.AddCurve("区域均值", list2, Color.Blue, SymbolType.Star);
           LineItem curve3= myPane.AddCurve("最大值", list3, Color.Red, SymbolType.Star);
            
            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
            //让X坐标名字竖着显示
            List<string> list = new List<string>();
            for (int i = 0; i < labels.Length; i++)
            {
                string value = "";
                for (int j = 0; j < labels[i].Length; j++)
                {
                    value += labels[i][j] + "\r\n";
                }
                list.Add(value);
            }
            string[] listvalue = new string[list.Count];
            for (int k = 0; k < list.Count; k++)
            {
                listvalue[k] = list[k];
            }

            myPane.XAxis.Scale.TextLabels = listvalue; //X轴文本取值
           double offset2 = 1;
            // Loop to add text labels to the points
            for (int i = 0; i < listvalue.Length; i++)
            {
                // Get the pointpair
                PointPair pt = curve.Points[i];
                // Create a text label from the Y data value

                TextObj text = new TextObj(pt.Y.ToString().Substring(0, pt.Y.ToString().IndexOf(".")+3), pt.X, pt.Y + offset2, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Black;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }
            for (int i = 0; i < listvalue.Length; i++)
            {
                // Get the pointpair
                PointPair pt = curve2.Points[i];
                // Create a text label from the Y data value
                TextObj text = new TextObj(pt.Y.ToString().Substring(0, pt.Y.ToString().IndexOf(".") + 3), pt.X, pt.Y + offset2, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Blue;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }
            for (int i = 0; i < listvalue.Length; i++)
            {
                // Get the pointpair
                PointPair pt = curve3.Points[i];
                // Create a text label from the Y data value
                TextObj text = new TextObj(pt.Y.ToString().Substring(0, pt.Y.ToString().IndexOf(".") + 3), pt.X, pt.Y + offset2, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Red;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }
            myPane.XAxis.MajorTic.IsBetweenLabels = true;
            myPane.XAxis.Type = AxisType.Text;   //X轴类型


            zgc.AxisChange();

            Refresh();
        }

猜你喜欢

转载自blog.csdn.net/A873054267/article/details/86346600