public string Draw(BaseChartSettings settings) { StringBuilder rst = new StringBuilder(); var paras = (BarChartSettings)settings; //绘制标题 StringBuilder title = new StringBuilder(); title.Append(" <title>" + paras.Title + "</title> "); title.Append(" <g> "); title.Append(" <text x=\"287\" y=\"246\" font-size=\"18\">" + paras.XTitle + "</text> "); title.Append(" <text x=\"52\" y=\"150\" font-size=\"18\" transform=\"rotate(-90,52,150)\">" + paras.YTitle + "</text> "); title.Append(" <text x=\"239\" y=\"24\" font-size=\"24\">" + paras.Title + "</text> "); title.Append(" </g> "); //绘制x轴和y轴的刻度 StringBuilder scale = new StringBuilder(); int xScaleCount = paras.Bars.Count; int xScaleSpace = 550 / xScaleCount; string xScale = string.Empty; for (int i = 0; i < xScaleCount; i++) { xScale += " v7 m" + xScaleSpace.ToString() + ",-7 "; } scale.Append(" <g id=\"coordinate\" stroke=\"#003f7f\" fill=\"white\"> "); scale.Append(" <path id=\"xy\" d=\"M90,200 h550 m-550,0 v-160\"></path> "); scale.Append(" <path id=\"xy-2\" d=\"M90,40 m550,0 v160\" stroke-dasharray=\"1,5\"></path> "); scale.Append(" <path id=\"lines\" d=\"M90,40 h550 m-550,32 h550 m-550,32 h550 m-550,32 h550 m-550,32 h550 m-550,32\" stroke-dasharray=\"1,5\"></path> "); scale.Append(" <path id=\"ys\" d=\"M90,40 h-7 m7,32 h-7 m7,32 h-7 m7,32 h-7 m7,32 h-7 m7,32 h-7 m7,32\"></path> "); scale.Append(" <path id=\"xs\" d=\"M90,200 v7 m" + xScaleSpace.ToString() + ",-7 " + xScale + "\"></path> "); scale.Append(" </g> "); //绘制y轴的坐标 int maxVal = paras.Bars.Max(); int yInterval = maxVal / 5 + 1; StringBuilder yCoordinate = new StringBuilder(); yCoordinate.Append(" <g id=\"yt\" direction=\"rtl\"> "); yCoordinate.Append(" <text x=\"81\" y=\"44\" font-size=\"14\">" + (yInterval * 5).ToString() + "</text> "); yCoordinate.Append(" <text x=\"81\" y=\"76\" font-size=\"14\">" + (yInterval * 4).ToString() + "</text> "); yCoordinate.Append(" <text x=\"81\" y=\"108\" font-size=\"14\">" + (yInterval * 3).ToString() + "</text> "); yCoordinate.Append(" <text x=\"81\" y=\"140\" font-size=\"14\">" + (yInterval * 2).ToString() + "</text> "); yCoordinate.Append(" <text x=\"81\" y=\"172\" font-size=\"14\">" + (yInterval * 1).ToString() + "</text> "); yCoordinate.Append(" </g> "); //绘制x轴的坐标 double xInterval = (double)paras.XMax / xScaleCount; StringBuilder xCoordinate = new StringBuilder(); xCoordinate.Append(" <g id=\"xt\"> "); xCoordinate.Append(" <text x=\"86\" y=\"210\" font-size=\"14\" transform=\"rotate(90,86,210)\">0</text> "); for (int i = 0; i < xScaleCount; i++) { int xp = 86 + xScaleSpace * (i + 1); double xc = xInterval * (i + 1); xCoordinate.Append(" <text x=\"" + xp.ToString() + "\" y=\"210\" font-size=\"14\" transform=\"rotate(90," + xp.ToString() + ",210)\">" + xc.ToString() + "</text> "); } xCoordinate.Append(" </g> "); //绘制矩形 StringBuilder bar = new StringBuilder(); StringBuilder rect = new StringBuilder(); StringBuilder text = new StringBuilder(); int rectIdx = 0; foreach (var item in paras.Bars) { int xRect = 90 + rectIdx * xScaleSpace; double rectHeight = ((double)32 / yInterval) * item; double yRect = 200 - rectHeight; int rectWidth = xScaleSpace - 2; string rectID = "R" + rectIdx.ToString() + "_" + DateTime.Now.Ticks.ToString(); rect.Append("<rect id=\"" + rectID + "\" x=\"" + xRect.ToString() + "\" y=\"" + yRect.ToString() + "\" width=\"" + rectWidth.ToString() + "\" height=\"" + rectHeight.ToString() + "\"></rect>"); text.Append(" <text x=\"" + xRect.ToString() + "\" y=\"" + (yRect - 6).ToString() + "\" display=\"none\"> " + item.ToString() + " "); text.Append(" <set attributeName=\"display\" from=\"none\" to=\"block\" begin=\"" + rectID + ".mouseover\" end=\"" + rectID + ".mouseout\"></set> "); text.Append(" </text> "); rectIdx++; } bar.Append(" <g id=\"bar\" fill=\"red\"> "); bar.Append(rect.ToString()); bar.Append(" </g> "); bar.Append(" <g id=\"label\"> "); bar.Append(text.ToString()); bar.Append(" </g> "); rst.Append(" <svg x=\"0\" y=\"0\" width=\"" + paras.Width.ToString() + "\" height=\"" + paras.Height.ToString() + "\" viewBox=\"0,0,650,250\" xmlns=\"http://www.w3.org/2000/svg\"> "); rst.Append(title.ToString()); rst.Append(scale.ToString()); rst.Append(yCoordinate.ToString()); rst.Append(xCoordinate.ToString()); rst.Append(bar.ToString()); rst.Append(" </svg> "); return rst.ToString(); }
public string Draw(BaseChartSettings settings) { throw new NotImplementedException(); }