Search code examples
c#mschart

Chart with Datasource Not Showing Correct Values


My chart doesn't seem to display the right values when it's a really small number (less than one). When I have big values (greater than one) it seems to chart and scale everything just fine. Any idea what I'm doing wrong?

My Charting Code:

private void do_chart_Conc(RunningTests rt, Chart c)
{
    c.Series.Clear();
    set_chart_alignment(c);

    DataTable dt = SQL.get_Conc(rt);

    c.DataSource = dt;

    Series s = new Series("Conc");
    s.XValueMember = "Time_Stamp";
    s.YValueMembers = "Conc";
    s.ChartType = SeriesChartType.Line;
    s.BorderWidth = 2;
    s.MarkerSize = 5;
    s.MarkerStyle = MarkerStyle.Circle;
    s.IsValueShownAsLabel = true;
    s.Label = "#VALY{0.0000}";

    c.ChartAreas[0].AxisY.IsStartedFromZero = false;
    c.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd\nHH:mm:ss";
    c.ChartAreas[0].AxisY.LabelStyle.Format = "0.0000";
    c.ChartAreas[0].RecalculateAxesScale();

    c.Series.Add(s);
    c.Legends.Clear();
}

My SQL Code:

static public DataTable get_Conc(RunningTests rt)
{
    DataTable dt = new DataTable();

    using (SqlConnection cs = new SqlConnection(connString))
    {
        string query = string.Empty;

        if (rt.StopTime.Ticks > 0)
        {
            query = string.Format("SELECT Time_Stamp, RawConc FROM Test WHERE Unit_ID = '{0}' AND Time_Stamp > '{1}' AND Time_Stamp < '{2}'", rt.Unit_ID, rt.StartTime.Ticks, rt.StopTime.Ticks);
        }
        else
        {
            query = string.Format("SELECT Time_Stamp, RawConc FROM Test WHERE Unit_ID = '{0}' AND Time_Stamp > '{1}'", rt.Unit_ID, rt.StartTime.Ticks);
        }

        SqlCommand cmd = new SqlCommand(query, cs);

        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            da.Fill(dt);
        }
    }

    //Person stored the date time to ticks, have to convert back to DateTime
    DataTable dtCloned = new DataTable();
    dtCloned.Clear();
    dtCloned.Columns.Add("Time_Stamp", typeof(DateTime));
    dtCloned.Columns.Add("Conc", typeof(int));

    foreach (DataRow dr in dt.Rows)
    {
        DataRow r = dtCloned.NewRow();
        r[0] = new DateTime((long)dr[0]);
        r[1] = dr[1];

        dtCloned.Rows.Add(r);
    }

    dtCloned.DefaultView.Sort = "Time_Stamp DESC";
    dtCloned = dtCloned.DefaultView.ToTable();

    return dtCloned;
}

Example Chart I'm getting: Chart 1 Zoomed: Chart Zoomed

The example Data: Data Table

I would like it to chart the actual values and display them (instead of zero). IE: -0.0021


Solution

  • You are losing precision because you are feeding in a table with y-values as int.

    Change

    dtCloned.Columns.Add("Conc", typeof(int));
    

    to

    dtCloned.Columns.Add("Conc", typeof(double));
    

    and all should be well..