MS Chart 控件 - 优化显示“零”的方法折线图类型的 Y 值列

发布于 2024-09-06 05:23:41 字数 805 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2024-09-13 05:23:41

如果 tblMonth.DefaultCount 始终为零,那么为什么不呢:

ISNULL (realData.Count, 0)

我猜您的查询如下所示:

SELECT
  sales_month = DATENAME ( month, s.sale_date )
  , sales_month_n = DATEPART( month, s.sale_date)
  , salesperson = e.employee_name
  , numb_of_sales = COUNT ( 1 )

FROM
 sales s
 JOIN employee e
   ON s.employee_id_salesperson = e.employee_id
WHERE s.sale_date >= '1/1/2010' and s.sale_date < '1/1/2011'
GROUP BY 
  DATENAME ( month, s.sale_date ), e.employee_name, DATEPART( month, s.sale_date)

-- 效果很好,直到您有员工在 6 月、7 月和 8 月起飞。您仍然希望它们出现在结果集中,但销售额为零

CREATE TABLE @tblMonth (myMonth nvarchar(10), n tinyint) 
    INSERT INTO @tblMonth VALUES ('January', 1) 
    ... 
    INSERT INTO @tblMonth VALUES ('December', 12)


SELECT
    all_possibles.sales_month
    , all_possibles.salesperson
    , ISNULL ( actual.numb_of_sales, 0 )
FROM 
    (
        SELECT
          sales_month = myMonth
          , sales_month_n = n
          , salesperson = e.employee_name
        FROM
            employee e
            ,@tblMonth M
    )all_possibles
    LEFT JOIN 
    (
        SELECT
          sales_month_n = DATEPART( month, s.sale_date)
          , salesperson = e.employee_name
          , numb_of_sales = COUNT ( 1 )
        FROM
         sales s
         JOIN employee e
           ON s.employee_id_salesperson = e.employee_id
        WHERE s.sale_date >= '1/1/2010' and s.sale_date < '1/1/2011'
        GROUP BY 
           e.employee_name, DATEPART( month, s.sale_date)
    )actuals
        ON 
        (
            all_possibles.salesperson = actuals.salesperson
            AND all_possibles.sales_month_n = actuals.sales_month_n
        )

对于冗长的答案,抱歉。希望您感到放心,您走在正确的道路上。祝你好运!

至于kludgy - 我使用实用程序/资源数据库中的永久表而不是@tblMonth。它已经填充了诸如一天中每一分钟的记录之类的内容,或者在本例中是一年中每个月的记录。这些类型的表,忘记它们叫什么——也许是事实表的一种形式?在查找或填补数据空白时非常有用。

If tblMonth.DefaultCount will always be zero, then why not:

ISNULL (realData.Count, 0)

Here's what I am guessing your query looks like:

SELECT
  sales_month = DATENAME ( month, s.sale_date )
  , sales_month_n = DATEPART( month, s.sale_date)
  , salesperson = e.employee_name
  , numb_of_sales = COUNT ( 1 )

FROM
 sales s
 JOIN employee e
   ON s.employee_id_salesperson = e.employee_id
WHERE s.sale_date >= '1/1/2010' and s.sale_date < '1/1/2011'
GROUP BY 
  DATENAME ( month, s.sale_date ), e.employee_name, DATEPART( month, s.sale_date)

--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

CREATE TABLE @tblMonth (myMonth nvarchar(10), n tinyint) 
    INSERT INTO @tblMonth VALUES ('January', 1) 
    ... 
    INSERT INTO @tblMonth VALUES ('December', 12)


SELECT
    all_possibles.sales_month
    , all_possibles.salesperson
    , ISNULL ( actual.numb_of_sales, 0 )
FROM 
    (
        SELECT
          sales_month = myMonth
          , sales_month_n = n
          , salesperson = e.employee_name
        FROM
            employee e
            ,@tblMonth M
    )all_possibles
    LEFT JOIN 
    (
        SELECT
          sales_month_n = DATEPART( month, s.sale_date)
          , salesperson = e.employee_name
          , numb_of_sales = COUNT ( 1 )
        FROM
         sales s
         JOIN employee e
           ON s.employee_id_salesperson = e.employee_id
        WHERE s.sale_date >= '1/1/2010' and s.sale_date < '1/1/2011'
        GROUP BY 
           e.employee_name, DATEPART( month, s.sale_date)
    )actuals
        ON 
        (
            all_possibles.salesperson = actuals.salesperson
            AND all_possibles.sales_month_n = actuals.sales_month_n
        )

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文