2012-08-02 102 views
3

我使用HighCharts HighStock版本在圖表中創建一系列數據。我在柱狀圖頂部有一個燭形圖,位於柱狀圖頂部。燭臺點是可點擊的。我想添加一個標誌到他們剛點擊的蠟燭圖上的點。HighStock HighCharts點擊事件設置標記

下面是一些我試着打代碼:

// create the chart 
     chart = new Highcharts.StockChart({ 
      chart: { 
       renderTo: 'container', 
       alignTicks: false 
      }, 

      rangeSelector: { 
       selected: 1 
      }, 

      title: { 
       text: 'DJIA Historical' 
      }, 

      yAxis: [{ 
       title: { 
        text: 'OHLC' 
       }, 
       height: 300, 
       lineWidth: 2 
      }, { 
       title: { 
        text: 'Volume' 
       }, 
       top: 400, 
       height: 100, 
       offset: 0, 
       lineWidth: 2 
      }, { 
       title: { 
        text: 'MACD' 
       }, 
       top: 520, 
       height: 100, 
       offset: 0, 
       lineWidth: 1 
      }], 

      series: [{ 
       type: 'candlestick', 
       name: 'DJIA', 
       data: ohlc, 
       events: { 
        click: function(event) { 

         console.log(chart); 
         chart.series[6].setData(event.point); 
        } 
       }, 
       dataGrouping: { 
        units: groupingUnits 
       } 
      }, { 
       type: 'column', 
       name: 'Volume', 
       data: volume, 
       yAxis: 1, 
       dataGrouping: { 
        units: groupingUnits 
       } 
      }, { 
       type: 'column', 
       name: 'Histogram', 
       pointPadding: 0, 
       groupPadding: 0, 
       data: histogram, 
       yAxis: 2, 
       color: '#666666' 
      }, { 
       type: 'line', 
       name: 'MACD', 
       pointPadding: 0, 
       groupPadding: 0, 
       data: macd, 
       yAxis: 2, 
       color: '#0000FF' 
      }, { 
       type: 'line', 
       name: 'Signal', 
       pointPadding: 0, 
       groupPadding: 0, 
       data: signal, 
       yAxis: 2, 
       color: '#000000' 
      }, { 
       type: 'flags', 
       name: 'Active Point', 
       data: [], 
       onSeries: ohlc, 
       shape: 'squarepin' 
      }] 
     }); 
    }) 

圖表不拋出任何JavaScript錯誤,但它不是創建標誌。至少,它沒有顯示出來。我希望基本上能夠在他們點擊的燭臺上畫上旗幟。如果他們點擊另一個地點,我想刪除舊標誌並在新點上繪製一個新標誌。我認爲這最好是通過添加和刪除系列中的數據完成的,但沒有多少運氣。

任何幫助,將不勝感激!

回答

6

首先,一個js-fiddle的例子將非常感謝您瞭解您的問題。 從我瞭解你的問題是最好的,試試這個(假設5是你的戰旗系列的指數,你的代碼好像是用指數6?)

click: function(event) { 
       this.chart.series[5].addPoint({ 
        x: event.point.x, 
        title: 'hey you clicked me' 
       }); 
      } 

我撥弄代碼@http://jsfiddle.net/U2Z2x/1/

由於您似乎只想顯示1個標誌,因爲您正確使用setData,這似乎是您最好的選擇,只是一個catch,setData需要一個數組,並且您傳遞一個值,也是點的格式需要稍微重做旗型系列

click: function(event) { 
       this.chart.series[5].setData([{ 
        x: event.point.x, 
        title: 'hey you clicked me' 
       }]); 

小提琴更新@http://jsfiddle.net/U2Z2x/2/

+0

您先生,是男士,謝謝! – Dexter 2012-08-03 18:28:54