MSChart - Forcing a line chart to draw from the Origin

烂漫一生 提交于 2019-12-13 03:21:26

问题


I have a databound chart running from the following MSSQL query -

"SELECT dateinvoiced AS Date, (SELECT SUM(value) FROM jobs WHERE dateinvoiced >= '" + 
new DateTime(year, month, 1).ToString("yyyy-MM-dd") + "' AND dateinvoiced <= 
j.dateinvoiced)/100 AS Revenue FROM jobs j WHERE dateinvoiced >= '" + new 
DateTime(year, month, 1).ToString("yyyy-MM-dd") + "' AND dateinvoiced <= '" + new 
DateTime(year, month, daysInMonth).ToString("yyyy-MM-dd") + "' GROUP BY dateinvoiced"

(Please forgive what I imagine is a strange way of handling dates, long story but I'll be changing them soon.)

The query is getting a cumulative total of money taken for work that the user's business has done, by day, over the calendar month. The chart itself compares this against a target value for the month, so the user can see progress towards their monthly goal. That side of things is all working fine, and the query is too. The problem comes at the start of the month.

The line drawn from the query above obviously starts from the first datapoint. If a user has not taken any money until, say, half way through a month, and then takes a large value, no line will be drawn on the chart because only one point is present. It will stay like this until the user takes money on a later day, at which point they can see a line.

What I'd like is a way of ensuring that there is always a line to see on the chart from the origin, even if the user only has data from one day.

The only way I can get this working myself is by checking to see if there is data on day 1 prior to the databind, and if there isn't simply inserting a record for a zero-value sale into the table. I'm sure you can see why this is not acceptable. Is there a way to manually add a point onto a databound table? I've tried and can't seem to do this. Failing that, is there some way to alter the SQL query to give me a value for every date, even though data isn't present for them all? Or any other methods at all?

Thanks.


回答1:


As suggested in the comment i believe it would be easier if you could set the series as Column type rather than Line since all days won't need to have a record associated with it.

In case you want to have all those dates made available then a query which would output all days would be good and then you could join it on your required set to get those values. substituting zero for values you don't have.

WITH AllDays
AS
(
    SELECT CAST('20120101' as datetime) AS DAYS
    UNION ALL
    SELECT DATEADD(dd, 1, DAYS)
    FROM AllDays
    WHERE DATEADD(dd, 1, DAYS) < cast('20120201' as datetime)
)
SELECT DAYS
FROM AllDays -- consider making the join here as you need


来源:https://stackoverflow.com/questions/8725323/mschart-forcing-a-line-chart-to-draw-from-the-origin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!