单击条形图后,在ng2-chart条形图中获取bar的值

时间:2017-05-26 20:27:54

标签: javascript ng2-charts

我的页面上显示了ng2图表条形图,但我希望获得该条形图的值,例如5,7,9。

我可以获得标签名称......但无法获得该值。

有没有人这样做过?

  public chartClicked(e:any):void {
       //e.active[0]._model.label gives the label.
    console.log(e);
  }

2 个答案:

答案 0 :(得分:1)

您可以使用以下解决方案:

lineChartData :您的数据源是否来自API,因此您必须添加单击时将使用的必要数据。

public chartClicked(e: any): void {
 if (e.active.length > 0) {
  const chart = e.active[0]._chart;
  const activePoints = chart.getElementAtEvent(e.event);

    if ( activePoints.length > 0) {

      const clickedElementIndex = activePoints[0]._index;
      const label = chart.data.labels[clickedElementIndex];

      console.log("serie from your dataset = " + activePoints[0]._model.datasetLabel);
      console.log("dataset index = " + activePoints[0]._datasetIndex);
      console.log("serie id from your data source = " + this.lineChartData[activePoints[0]._datasetIndex].labelId);
      console.log("serie from your data source = " + this.lineChartData[activePoints[0]._datasetIndex].label);
      console.log("label from your dataset = " + label);
    }
   }}

答案 1 :(得分:0)

你可以试试这个:

DECLARE @t TABLE(
Employee NVARCHAR(10)
,StartTime DATETIME
,EndTime DATETIME
)

INSERT INTO @t VALUES ('A', '2001-01-01 23:00:00', '2001-01-03 10:00:00')
                     ,('A', '2001-01-05 21:00:00', '2001-01-06 22:00:00')
                     ,('A', '2001-01-07 21:00:00', '2001-01-08 22:00:00')
                     ,('B', '2001-01-01 21:00:00', '2001-01-01 22:00:00')
                     ,('B', '2001-01-02 21:00:00', '2001-01-03 02:00:00')
                     ,('C', '2001-01-03 02:00:00', '2001-01-04 00:00:00');

WITH cte AS(
  SELECT 1 AS lvl, Employee, CONVERT(DATE, StartTime) StartTime_DATE, StartTime, EndTime
    FROM @t AS t
  UNION ALL
  SELECT lvl + 1 AS lvl, c.employee, DATEADD(d, 1, c.StartTime_DATE) StartTime_DATE, c.StartTime, c.EndTime
    FROM cte AS c
    WHERE DATEADD(d, 1, c.StartTime_DATE) < c.EndTime
),
cteCalc AS(
  SELECT *
        ,CONVERT(DATE, StartTime) AS StartDate
        ,CASE WHEN lvl > 1 THEN CONVERT(DATETIME,CONVERT(DATE, DATEADD(d, DATEDIFF(d, StartTime, StartTime_DATE), StartTime))) ELSE DATEADD(d, DATEDIFF(d, StartTime, StartTime_DATE), StartTime) END AS StartTimeNew
        ,ISNULL(CONVERT(datetime, LEAD(StartTime_DATE) OVER (PARTITION BY Employee, CONVERT(DATE, StartTime) ORDER BY StartTime_DATE)), EndTime) AS EndTimeNew
    FROM cte

)
SELECT Employee, StartTime_DATE AS StartDate, DATEDIFF(MINUTE, StartTimeNew, EndTimeNew)/60.0 AS WorkHours
  FROM cteCalc
  ORDER BY Employee, StartTime_DATE
  OPTION (MAXRECURSION 0)