使用開源Echarts爲Splunk打造類似語法驅動的分析可視化

Splunk是業內領先的機器數據平臺,有非常易用的用戶界面的可視化的選項。Splunk的可視化圖表是使用開源的highcharts構建的。但是Splunk內置的可視化選項不夠靈活,不能動態的進行數據到圖表的綁定。可喜的是,最新版本的Splunk提供了客戶自定義圖表的功能, 利用該功能,我們可以打造一個類似Tableau或者ggplot的語法驅動的可視化工具。

代碼在

主要邏輯代碼代碼是echarts_app/appserver/static/visualizations/echarts/src/visualization_source.js,不超過600行代碼,大家有興趣可以去看一下。

安裝比較簡單,拷貝echarts_app到SPLUNK_HOME/etc/apps目錄下,

然後在 echarts_app/appserver/static/visualizations/echarts/ 在運行以下命令:

npm install
npm run build

當然,你需要在本機事先安裝好Splunk的最近版本和npm,並設置SPLUNK_HOME的環境變量。

目錄下自帶了幾個數據的樣本,下面就詳細說明一下如何使用:

數據綁定

先解釋一下數據綁定,Splunk的SPL返回一個Table結構的數據表,利用該表我們可以把某一列數據綁定在某個可視化的選項上。

例如以下的SPL:

source="drinks.csv" 
| table country, wine_servings,  beer_servings, spirit_servings, total_litres_of_pure_alcohol 
| head 5

會返回這樣的數據表




我們用上表中每一列的索引來進行綁定,這裏country的索引是0,beer_servings的索引是2。

點擊可視化選項Format,可以看到,凡是名字中包含Binding的選項,都可以進行數據的綁定。,綁定時,直接輸入對應的逗號分割索引列表就好了,例如,要綁定第2和3列數據,輸入1,2

單軸

餅圖

SPL:

source="drinks.csv" 
| table country, wine_servings,  beer_servings, spirit_servings, total_litres_of_pure_alcohol 
| head 5

語法設置:

Coordinates -> Coordinates Type = Single Axis
Single Axis -> Axis Binding = 0  (country)
Data Series -> Data Type = Pie
Data Series -> Data Color Binding = 1 (wine_servings)

單軸散點圖

SPL:

source="drinks.csv" 
| table country, wine_servings,  beer_servings, spirit_servings, total_litres_of_pure_alcohol 

語法設置:

Coordinates -> Coordinates Type = Single Axis
Single Axis -> Axis Binding = 0  (country)
Data Series -> Data Type = Scatter
Data Series -> Data Color Binding = 1 (wine_servings)

X-Y 座標系

柱狀圖

SPL

source="drinks.csv" 
| table country, wine_servings,  beer_servings, spirit_servings, total_litres_of_pure_alcohol 
| head 10

語法設置:

Coordinates -> Coordinates Type = X-Y
X-Y Axis -> X-Axis Binding = 0 (country)
X-Y Axis -> X-Axis Type = Category
X-Y Axis -> Y-Axis Binding = 1,2 (wine_servings,  beer_servings)
X-Y Axis -> Y-Axis Type = Value
Data Series -> Data Type = Bar


堆疊(Stack)模式只要利用echarts的工具箱,點擊換爲堆疊/換爲平鋪,直接可以切換

修改 Data Series -> Data Type = Bar 可獲得折線(Line)圖


同樣可以切換爲堆積模式


折線圖和區域(Area)圖本質是一樣的,只需要選擇 Data Series -> Show Area = True/False

同樣的可以切換爲堆疊(stack)模式

對換X-Y軸的綁定可以得到Bar Chart
X-Y Axis -> X-Axis Binding = 1,2 (wine_servings,  beer_servings)
X-Y Axis -> X-Axis Type = Value
X-Y Axis -> Y-Axis Binding = 0 (country)
X-Y Axis -> Y-Axis Type = Category

散點圖

SPL

source="drinks.csv" 
| table country, wine_servings,  beer_servings, spirit_servings, total_litres_of_pure_alcohol 

語法設置:
Coordinates -> Coordinates Type = X-Y
X-Y Axis -> X-Axis Binding = 1 (wine_servings)
X-Y Axis -> X-Axis Type = Value
X-Y Axis -> Y-Axis Binding = 2 (beer_servings)
X-Y Axis -> Y-Axis Type = Value
Data Series -> Data Type = Scatter

增加對第四列的分析,綁定爲顏色
Data Series -> Data Color Binding = 3 (spirit_servings)

增加對第四列的分析,綁定爲數據點的大小
Data Series -> Data Size Binding = 4 (total_litres_of_pure_alcohol)

地圖

等值線圖 (Choropleth Map)
SPL

source="drinks.csv" 
| table country, wine_servings,  beer_servings, spirit_servings, total_litres_of_pure_alcohol 

語法設置:
Coordinates -> Coordinates Type = Geomap
Geomap -> Map Type = World
Geomap -> Geo Naming Binding = 0 (country)
Data Series -> Data Type = Map
Data Series -> Data Color Binding = 1 (wine_servings)



散點圖

SPL:

source="police_killings.csv" | table latitude,longitude,p_income

語法設置:
Coordinates -> Coordinates Type = Geomap
Geomap -> Map Type = World
Geomap -> Longitude and Latitude Binding = 1,0 (longitude,latitude)
Data Series -> Data Type = Scatter
Data Series -> Data Color Binding = 2 (p_income)

切換爲美國地圖 Geomap -> Map Type = USA


增加綁定數據p_income到點的大小

缺省內置了世界地圖,中國地圖和美國地圖,如果用戶需要增加其他地圖,需要調用echarts.registerMap方法來增加新的地圖。

其它設置

echart提供非常多的可視化的visual選項,這裏更多的爲了數據分析,並沒有提供很多和分析無關的選項。在Settings裏面,用戶可以選擇修改顏色綁定的默認值和數據點最大最小的範圍值(單位是像素)。


總結

相比起Splunk內置的圖表,使用語法驅動的圖表更爲靈活,但是用戶需要理解每一個綁定的含義,有一定的難度,但相信如果用戶熟悉話,應該是很好用的。歡迎大家來指教。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章