MS Chart 控件 - 优化显示“零”的方法折线图类型的 Y 值列
我正在使用 VS 2010 附带的 Microsoft Chart 扩展。这很适合我的需求,但我遇到了烦恼,正在寻找更优雅的解决方案。
绘制折线图时,为了获得连续的线,我需要所有 X 坐标的数据。我的数据集是按月计算的员工的销售数量,其中销售计数在 Y 上,月份在 X 上。如果没有返回 X 值的数据(即员工休假了一个月),就会出现问题……所以该线不连续。
我不确定图表控件中是否有我忽略的设置,但我的不优雅的解决方案是为员工没有发布销售的月份创建“假”零销售数据。
我使用 MS SQL 中的存储过程来创建数据集,其中每列代表一个月,每行代表一名员工。然后,我在图表控件中为每个员工创建一个新系列。
为了记录我的零销售月份,我在 SQL 中创建了一个临时表。
CREATE TABLE @tblMonth (myMonth nvarchar(10), defaultCount int)
INSERT INTO @tblMonth VALUES (‘January’, 0)
…
INSERT INTO @tblMonth VALUES (‘December’, 0)
然后,我对实际数据记录执行临时表的联接,并使用
ISNULL (realData.Count, tblMonth.defaultCount)
来获取“假”零销售数据。
这可行……但对我来说真的很笨拙……我情不自禁地觉得我忽略了一些更适合我目的的简单东西。再说一次,我已经完成了这项工作……但总是在寻找更好的做事方法并扩展我的知识库……因此,如果有人对如何更好地完成上述任务有建议,我希望得到一些建议或反馈。
I’m using Microsoft Chart extensions that ship with VS 2010. This suits my needs well, but I’ve run into an annoyance and am looking for a more elegant solution.
When charting a line graph, to achieve a continuous line, I require data for all X coordinates. My dataset is for number of sales by employee by month, where sales count is on the Y and month is on the X. The problem arises where no data is returned for an X value (ie. An employee took a month off)…so the line is not continuous.
I’m not sure if there is a setting I’ve overlooked in the Chart control, but my inelegant solution is to create ‘fake’ zero sales data for the months the employee posted no sales.
I’m using a stored procedure in MS SQL to create my dataset, where each column is a month and each row is for an employee. I create a new series in the Chart control for each employee then.
In order to capture my zero sales months, I have created a temp table in SQL.
CREATE TABLE @tblMonth (myMonth nvarchar(10), defaultCount int)
INSERT INTO @tblMonth VALUES (‘January’, 0)
…
INSERT INTO @tblMonth VALUES (‘December’, 0)
I then perform a join of my temp table on my actual data record and use
ISNULL (realData.Count, tblMonth.defaultCount)
To get my ‘fake’ zero sales data.
This works…but FEELS really kludgy to me…I can’t help but feel I’ve overlooked something simple that would better suit my purposes. Again, I’ve got this working…but always looking for better ways of doing things and expand my knowledge base….so if anyone has suggestions of how better to accomplish the above, I’d love some suggestions or feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 tblMonth.DefaultCount 始终为零,那么为什么不呢:
我猜您的查询如下所示:
-- 效果很好,直到您有员工在 6 月、7 月和 8 月起飞。您仍然希望它们出现在结果集中,但销售额为零
对于冗长的答案,抱歉。希望您感到放心,您走在正确的道路上。祝你好运!
至于kludgy - 我使用实用程序/资源数据库中的永久表而不是@tblMonth。它已经填充了诸如一天中每一分钟的记录之类的内容,或者在本例中是一年中每个月的记录。这些类型的表,忘记它们叫什么——也许是事实表的一种形式?在查找或填补数据空白时非常有用。
If tblMonth.DefaultCount will always be zero, then why not:
Here's what I am guessing your query looks like:
--which works fine, until you have an employee take off in June, July and August. You still want them to show up in the resultset, but with zero sales
Sorry for the long winded answer. Hope you feel reassured that you are on the right track. Good luck!
As to kludgy - instead of @tblMonth, I use permanent tables in a utility / resource database. it is already populated with things like a record for every minute in the day, or in this case, a record for every month in a year. These types of tables, forget what they are called - maybe a form of fact table? are VERY useful when looking for or filling in gaps in data.