Exemple #1
0
    /// <summary>
    /// generates a graph containing downloads per date
    /// </summary>
    /// <param name="index"></param>
    /// <param name="model_id"></param>
    /// <param name="labelX"></param>
    /// <param name="labelY"></param>
    /// <returns></returns>
    private HtmlGenericControl GenerateAssetGraph(int index, int model_id, out Label[] labelX, out Label[] labelY)
    {
        HtmlGenericControl svg = new HtmlGenericControl("svg");

        svg.Attributes["class"] = "asset-graph";

        string[] mat = ws.GetDownloadCountArray(model_id);

        labelX = new Label[5];
        labelY = new Label[5];

        for (int i = 0; i < labelX.Length; i++)
        {
            labelX[i] = new Label();
            labelY[i] = new Label();
        }

        if (mat.Length > 0)
        {
            Dictionary <DateTime, int> dateCount = new Dictionary <DateTime, int>();

            for (int i = 0; i < mat.Length / 2; i++)
            {
                dateCount.Add(ConvertToDate(mat[i]), int.Parse(mat[mat.Length / 2 + i]));
            }

            Dictionary <TimeSpan, int> timeCount = new Dictionary <TimeSpan, int>();

            //offset from first date

            DateTime reference = dateCount.First().Key;

            for (int i = 0; i < dateCount.Count; i++)
            {
                timeCount.Add(dateCount.ElementAt(i).Key.Subtract(reference), dateCount.ElementAt(i).Value);
            }

            //multiply years by 365 and months by 30

            HtmlGenericControl xAxis = new HtmlGenericControl("polyline");
            xAxis.Attributes["fill"]         = "none";
            xAxis.Attributes["stroke"]       = "black";
            xAxis.Attributes["stroke-width"] = "2";
            xAxis.Attributes["points"]       = "1,0 0,0 0,1";

            HtmlGenericControl[] marks = new HtmlGenericControl[10];

            for (int i = 0; i < marks.Length; i++)
            {
                marks[i] = new HtmlGenericControl("polyline");
                marks[i].Attributes["fill"]         = "none";
                marks[i].Attributes["stroke"]       = "lightgray";
                marks[i].Attributes["stroke-width"] = "2";
                marks[i].Attributes["points"]       = "0,0 1,0";
            }

            DateTime first     = dateCount.First().Key;
            int      max_value = CalcMaxCount(timeCount);
            TimeSpan offset    = CalcAxisOffset(dateCount);

            for (int i = 0; i < labelX.Length; i++)
            {
                labelX[i]          = new Label();
                labelX[i].Text     = ((i + 1) * max_value / labelX.Length).ToString();
                labelX[i].CssClass = "label-x" + i;
            }

            for (int i = 0; i < labelY.Length; i++)
            {
                DateTime date = first;
                for (int k = 0; k <= i; k++)
                {
                    date = date.Add(offset);
                }
                labelY[i]          = new Label();
                labelY[i].Text     = (date).ToShortDateString();
                labelY[i].CssClass = "label-y" + i;
            }


            HtmlGenericControl polyline = new HtmlGenericControl("polyline");
            polyline.Attributes["fill"]         = "none";
            polyline.Attributes["stroke"]       = "rgb(0, 255, 0)";
            polyline.Attributes["stroke-width"] = "3";

            polyline.Attributes["points"] = "";
            for (int i = 0; i < dateCount.Count; i++)
            {
                polyline.Attributes["points"] += timeCount.ElementAt(i).Key.TotalDays + "," + timeCount.ElementAt(i).Value + " ";
            }

            svg.Controls.Add(polyline);
            svg.Controls.Add(xAxis);

            for (int i = 0; i < marks.Length; i++)
            {
                svg.Controls.Add(marks[i]);
            }
        }
        else
        {
            HtmlGenericControl xAxis = new HtmlGenericControl("polyline");
            xAxis.Attributes["fill"]   = "none";
            xAxis.Attributes["stroke"] = "black";

            xAxis.Attributes["points"] = "1,0 0,0 0,1";

            HtmlGenericControl[] marks = new HtmlGenericControl[10];

            for (int i = 0; i < marks.Length; i++)
            {
                marks[i] = new HtmlGenericControl("polyline");
                marks[i].Attributes["fill"]   = "none";
                marks[i].Attributes["stroke"] = "lightgray";

                marks[i].Attributes["points"] = "0,0 1,0";
            }

            HtmlGenericControl polyline = new HtmlGenericControl("polyline");
            polyline.Attributes["fill"]   = "none";
            polyline.Attributes["stroke"] = "rgb(0, 255, 0)";


            polyline.Attributes["points"] = "0,0 0,0 ";

            svg.Controls.Add(polyline);

            svg.Controls.Add(xAxis);

            for (int i = 0; i < marks.Length; i++)
            {
                svg.Controls.Add(marks[i]);
            }
        }

        return(svg);
    }