Widget:StatsGraph: Difference between revisions
Jump to navigation
Jump to search
m Changed to use "currentScript" into console |
Optional fill and stroke params, made ID unneeded |
||
Line 1: | Line 1: | ||
<includeonly><div | <includeonly><div class="stats-graph-container"><script> | ||
function calcCoordinate(degree, min, max, val) { | function calcCoordinate(degree, min, max, val) { | ||
Line 27: | Line 27: | ||
} | } | ||
function buildGraph( | function buildGraph(dataComplete) { | ||
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | ||
var svgNS = svg.namespaceURI; | var svgNS = svg.namespaceURI; | ||
Line 38: | Line 38: | ||
/* This pretty much defines the base net lines */ | /* This pretty much defines the base net lines */ | ||
var defs = document.createElementNS(svgNS, 'defs'); | var defs = document.createElementNS(svgNS, 'defs'); | ||
/* Wedge id has to be unique */ | |||
var wedgeId = "wedge-" + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); | |||
return v.toString(16); | |||
}); | |||
var wedge = document.createElementNS(svgNS, 'g'); | var wedge = document.createElementNS(svgNS, 'g'); | ||
wedge.setAttribute('id', | wedge.setAttribute('id', wedgeId); | ||
var directionCount = dataComplete[0].data.length; | var directionCount = dataComplete[0].data.length; | ||
Line 71: | Line 77: | ||
for (var dataIdx=0; dataIdx<directionCount; dataIdx++) { | for (var dataIdx=0; dataIdx<directionCount; dataIdx++) { | ||
var propertyLine = document.createElementNS(svgNS, 'use'); | var propertyLine = document.createElementNS(svgNS, 'use'); | ||
propertyLine.setAttributeNS(svgLinkNS, "href", "# | propertyLine.setAttributeNS(svgLinkNS, "href", "#" + wedgeId); | ||
propertyLine.setAttribute('transform', "rotate(" + (degreeSpacing * dataIdx) + ")"); | propertyLine.setAttribute('transform', "rotate(" + (degreeSpacing * dataIdx) + ")"); | ||
gDrawing.appendChild(propertyLine ); | gDrawing.appendChild(propertyLine ); | ||
Line 79: | Line 85: | ||
var polygonParams = dataComplete[dataIdx]; | var polygonParams = dataComplete[dataIdx]; | ||
var datapresentation = document.createElementNS(svgNS, 'polygon'); | var datapresentation = document.createElementNS(svgNS, 'polygon'); | ||
datapresentation.setAttribute('class', polygonParams.class); | datapresentation.setAttribute('class', polygonParams.class ? polygonParams.class : "graph-orange"); | ||
if ('fill' in polygonParams) datapresentation.setAttribute('fill', polygonParams.fill); | |||
if ('stroke' in polygonParams) datapresentation.setAttribute('stroke', polygonParams.stroke); | |||
datapresentation.setAttribute('points', buildPolygonString(polygonParams.data)); | datapresentation.setAttribute('points', buildPolygonString(polygonParams.data)); | ||
gDrawing.appendChild(datapresentation); | gDrawing.appendChild(datapresentation); | ||
Line 95: | Line 103: | ||
svg.appendChild(gDrawing); | svg.appendChild(gDrawing); | ||
var | var container = document.currentScript.parentNode; | ||
container.appendChild(svg); | |||
} | } | ||
var graphData = JSON.parse(`<!--{$data|escape:'html'}-->`.replace(/"/gi, '"')); | var graphData = JSON.parse(`<!--{$data|escape:'html'}-->`.replace(/"/gi, '"')); | ||
buildGraph( | buildGraph(graphData);</script></div></includeonly><noinclude>{{Documentation}}</noinclude> | ||
Revision as of 22:50, 2 October 2019
Widget to create an SVG image containing a radar chart. It is configured with a JSON string.
Parameters
Parameter name | Required? | Default value | Description |
---|---|---|---|
data |
Yes | (n/a) | A JSON String containing all values. Please make sure that
See the testcases for examples how to configure the JSON string. |
Usage
See the template's testcases:
Small graph using default colors | |
---|---|
Wiki code | {{#widget:StatsGraph |data=[ { "data": [ { "label": "RoF", "min": 12, "max": 37, "value": 14 }, { "label": "Acc", "min": 34, "max": 68, "value": 37 }, { "label": "Dmg", "min": 9, "max": 12, "value": 11 } ] } ]}} |
Main version | |
Sandbox version | Error in widget StatsGraph/sandbox: Unable to load template 'wiki:StatsGraph/sandbox'
|
Graph showing two graphs, second graph with different color | |
---|---|
Wiki code | {{#widget:StatsGraph |data=[ { "data": [ { "label": "RoF", "min": 12, "max": 37, "value": 32 }, { "label": "Acc", "min": 12, "max": 37, "value": 32 }, { "label": "Dmg", "min": 12, "max": 37, "value": 32 } ] }, { "fill": "blue", "stroke": "darkblue", "data": [ { "label": "RoF", "min": 12, "max": 37, "value": 34 }, { "label": "Acc", "min": 34, "max": 68, "value": 51 }, { "label": "Dmg", "min": 9, "max": 12, "value": 9 } ] } ]}} |
Main version | |
Sandbox version | Error in widget StatsGraph/sandbox: Unable to load template 'wiki:StatsGraph/sandbox'
|
Graph with more properties | |
---|---|
Wiki code | {{#widget:StatsGraph |data=[ { "data": [ { "label": "RoF", "min": 12, "max": 37, "value": 14 }, { "label": "Acc", "min": 34, "max": 68, "value": 37 }, { "label": "Dmg", "min": 9, "max": 16, "value": 11 }, { "label": "Dmg1", "min": 9, "max": 16, "value": 12 }, { "label": "Dmg2", "min": 9, "max": 16, "value": 13 }, { "label": "Dmg3", "min": 9, "max": 16, "value": 14 } ] } ]}} |
Main version | |
Sandbox version | Error in widget StatsGraph/sandbox: Unable to load template 'wiki:StatsGraph/sandbox'
|
The above documentation is transcluded from Widget:StatsGraph/doc. (edit | history) |