Skip to content

Instantly share code, notes, and snippets.

@Himujjal
Last active November 27, 2017 06:04
Show Gist options
  • Select an option

  • Save Himujjal/0a31f2012c6497c638cb07416e4bd5a9 to your computer and use it in GitHub Desktop.

Select an option

Save Himujjal/0a31f2012c6497c638cb07416e4bd5a9 to your computer and use it in GitHub Desktop.
Add the following to Creating table function and creating chart
import "../styles/a-simple-linear-regression.less";
// Start Writing Javascript below this line
import math from 'mathjs'
import FusionCharts from 'fusioncharts'
require("fusioncharts/fusioncharts.charts")(FusionCharts);
// This is where we will write the algorithms
const $ = (id) => document.getElementById(id),
cE = (ele) => document.createElement(ele),
cTN = (text) => document.createTextNode(text)
Array.prototype.max = function() {return this.reduce((acc,e)=>Math.max(acc,e))}
Array.prototype.min = function() {return this.reduce((acc,e)=>Math.min(acc,e))}
let fillTable = (headerCols, mat, tableId) => {
let headerRow = cE('tr')
headerCols.forEach(headerText=>{
let headerCol = cE('th')
headerCol.appendChild(cTN(headerText))
headerRow.appendChild(headerCol)
})
$(tableId).appendChild(headerRow)
if(math.matrix(mat).size()[0] === 1) {
let row = cE('tr')
let cols = mat.map(ele=>cE('td'))
mat.forEach((ele, i)=>{
let text = cTN(ele)
cols[i].appendChild(text)
row.appendChild(cols[i])
})
$(tableId).appendChild(row)
} else {
let matrix = math.transpose(mat)
matrix.forEach((ele,i)=>{
let row = cE('tr')
let cols = [...Array(matrix[0].length)].map(ele=>cE('td'))
cols.forEach((col,j) => {
col.appendChild(cTN(matrix[i][j]))
row.appendChild(col)
$(tableId).appendChild(row)
})
})
}
}
let renderScatterPlot = (renderId, options, categoryArray, dataset) => {
let {caption, subCaption} = options
new FusionCharts({
type: 'scatter',
renderAt: renderId,
width: '80%',
height: '400',
dataFormat: 'json',
dataSource: {
chart: Object.assign({}, options, {
showValues: "0",
labelDisplay: 'auto',
showHoverEffect: "1",
canvasPadding: "10",
showaxislines: "1",
anchorRadius: "4",
setAdaptiveYMin: '1',
anchorHoverRadius: "8",
theme: "hulk-light"
}),
categories: [{category: categoryArray.map(d=>({label:String(d), x: String(d)}))}],
dataset: dataset.map((set,i)=>({
seriesname: set.name,
color: set.color,
drawLine: set.drawLine,
anchorBgColor: set.color,
data: set.data[0].map((x,i)=>({x, y: set.data[1][i]}))
}))
}
}).render(renderId)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment