HTML Content to Javascript Code - javascript
I am trying to take the content in a div tag and turn it into Javascript code. The reason for this is to take the div information and convert it into a Highchart data series.
HTML Content
<div id="data">{name: 'Point 1',x: Date.UTC(2014, 11, 1),x2: Date.UTC(2014, 11, 8),y: 0},{name: 'Point 2',x: Date.UTC(2014, 12, 1),x2: Date.UTC(2014, 12, 8),y: 0},</div>
Javascript/snippet Content
$(function () {
var newdata = $("#data");
(function (H) {
var defaultPlotOptions = H.getOptions().plotOptions,
columnType = H.seriesTypes.column,
each = H.each;
defaultPlotOptions.xrange = H.merge(defaultPlotOptions.column, {});
H.seriesTypes.xrange = H.extendClass(columnType, {
type: 'xrange',
parallelArrays: ['x', 'x2', 'y'],
requireSorting: false,
animate: H.seriesTypes.line.prototype.animate,
/**
* Borrow the column series metrics, but with swapped axes. This gives free access
* to features like groupPadding, grouping, pointWidth etc.
*/
getColumnMetrics: function () {
var metrics,
chart = this.chart;
function swapAxes() {
each(chart.series, function (s) {
var xAxis = s.xAxis;
s.xAxis = s.yAxis;
s.yAxis = xAxis;
});
}
swapAxes();
this.yAxis.closestPointRange = 1;
metrics = columnType.prototype.getColumnMetrics.call(this);
swapAxes();
return metrics;
},
translate: function () {
columnType.prototype.translate.apply(this, arguments);
var series = this,
xAxis = series.xAxis,
metrics = series.columnMetrics;
H.each(series.points, function (point) {
var barWidth = xAxis.translate(H.pick(point.x2, point.x + (point.len || 0))) - point.plotX;
point.shapeArgs = {
x: point.plotX,
y: point.plotY + metrics.offset,
width: barWidth,
height: metrics.width
};
point.tooltipPos[0] += barWidth / 2;
point.tooltipPos[1] -= metrics.width / 2;
});
}
});
/**
* Max x2 should be considered in xAxis extremes
*/
H.wrap(H.Axis.prototype, 'getSeriesExtremes', function (proceed) {
var axis = this,
dataMax = Number.MIN_VALUE;
proceed.call(this);
if (this.isXAxis) {
each(this.series, function (series) {
each(series.x2Data || [], function (val) {
if (val > dataMax) {
dataMax = val;
}
});
});
if (dataMax > Number.MIN_VALUE) {
axis.dataMax = dataMax;
}
}
});
}(Highcharts));
// THE CHART
$('#container').highcharts({
chart: {
type: 'xrange'
},
title: {
text: 'Highcharts X-range study'
},
plotOptions: {
series: {
events: {
mouseOver: function () {
var cur = this;
Highcharts.each(this.chart.series, function (series) {
if (series !== cur) {
series.group.animate({
opacity: 0.2
}, {
duration: 150
});
} else {
series.group.animate({
opacity: 1
}, {
duration: 150
});
}
});
},
mouseOut: function () {
this.group.animate({
opacity: 1
}, {
duration: 150
});
}
}
}
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: '',
categories: [],
},
series: [$(newdata).text()]
}]
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="data">{name: 'Point 1',x: Date.UTC(2014, 11, 1),x2: Date.UTC(2014, 11, 8),y: 0},{name: 'Point 2',x: Date.UTC(2014, 12, 1),x2: Date.UTC(2014, 12, 8),y: 0},</div>
It pulls the content as text and does not create it as code. Can this be done?
If all of the contents of the #data element is properly formatted, all you'll actually have to do is grab the inner html of that element and parse it into JSON.
// This is the contents of #data as a String
var data_as_a_string = document.getElementById("data").innerHTML;
// And here it is "in javascript" as an Object
var data_as_an_object = JSON.parse( data_as_a_string );
you can try eval function
The eval() function evaluates JavaScript code represented as a string.
eval(string)
eval(" var s='hello'; alert(s);");
Related
Highcharts add new data to series after click
I'm trying to add a new point (to series data) on spline chart after clicking in the place where I'm clicked on the line. But point click event doesn't return xAxis, yAxis (only in pixels). I decide to calculate the difference between point pixels position and click, but point adds not on the click place. What I'm doing wrong? How to handle this? My JS var setDragStatus = function (status) { document.getElementById('dragstatus').innerHTML = status; }; Highcharts.chart('container', { title: { text: 'Spline Drag&Drop' }, plotOptions: { series: { turboThreshold: 4, minPointLength: 5, dragDrop: { draggableY: true, dragMaxY: 1, }, point: { events: { click: function (e) { let pointPlotX = e.point.plotX let pointPlotY = e.point.plotY let pointX = e.point.x let pointY = e.point.y let clickX = e.chartX let clickY = e.chartY let pointDiffX = clickX / pointPlotX let pointDiffY = clickY / pointPlotY let newPointX = pointX * pointDiffX let newPointY = pointDiffY * pointY this.series.addPoint([newPointX, newPointY]) } } }, } }, xAxis: { reversed: false, showFirstLabel: false, showLastLabel: true }, series: [ { name: 'spline top', data: [0, 0.3, 0.6, 1], type: 'spline' } ] } ); Result - https://jsfiddle.net/antiaf/1hfuyjbr/
To calculate x and y values you can use toValue Axis method: plotOptions: { series: { ..., point: { events: { click: function(e) { let series = this.series, yAxis = series.yAxis, xAxis = series.xAxis, newPointX = xAxis.toValue(e.chartX), newPointY = yAxis.toValue(e.chartY); this.series.addPoint([newPointX, newPointY]) } } } } } Live demo: https://jsfiddle.net/BlackLabel/hg81o4ej/ API Reference: https://api.highcharts.com/class-reference/Highcharts.Axis#toValue
Spline Graph with diagonally fixed values with 0,0 and ploting remaing same
We are using Spline Graph for our game in which we are facing issue with x and y axis value which we need to put 0,0 and save the values from initially till end as we need to all plotting from start till end of the value. Check Live Demo Here JavaScript Code <script> var a = 1; var b = 1; var factor = 1.2; $(document).ready(function () { Highcharts.chart('container', { chart: { type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, events: { load: function () { // set up the updating of the chart each second var series = this.series[0]; setInterval(function () { b = b*1.2; console.log(b); var x = a; // current time var y = b; a++; series.addPoint([x, y], true, true); }, 700); } } }, title: { text: 'Live random data' }, xAxis: { type: 'number', min: 0, tickInterval: 2 }, yAxis: { title: { text: 'Value' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function () { return '<b>X: ' + this.x+', Y:'+this.y; } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ name: 'Random data', data: (function () { // generate an array of random data var data = [],i; for (i = 1; i <= 19; i++) { b = b*factor; data.push({ x: a, y: b }); a++; } return data; }()) }] }); });
The following code draws a curve line from the origin (0,0) to the end point which gets updated on the interval. You needed to make the shift variable false in the addPoint call. Higchart docs $(document).ready(function () { var a = 1; var b = 1; var factor = 1.2; Highcharts.chart('container', { chart: { type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, events: { load: function () { // set up the updating of the chart each second var series = this.series[0]; setInterval(function () { b = b*1.2; var x = a; // current time var y = b; a++; // Add new end point series.addPoint([x, y], true, false); }, 700); } } }, title: { text: 'Live random data' }, xAxis: { type: 'number', min: 0, tickInterval: 2 }, yAxis: { title: { text: 'Value' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function () { return '<b>X: ' + this.x+', Y:'+this.y; } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ name: 'Random data', data: (function () { // generate an array of random data // Add point at origin and last point of series var data = [{x:0,y:0}],i; for (i = 1; i <= 19; i++) { b = b*factor; a++ data.push({ x: a, y: b }); } return data; }()) }] }); }); Updated JsFiddle
jqplot chart with data from variable and data from JSON
I'm trying to load data into a jqplot chart via variable, but it's only displaying the first value. I'm lost at to why is doing this. JavaScript: $(document).ready(function () { var sin = [[20, 10, 0, 10, 15, 25, 35, 50, 48, 45, 35, 30, 15, 10]]; var plot = $.plot($(".chart"), [{ data: sin, label: "sin(x)", color: "#ee7951" }], { series: { lines: { show: true }, points: { show: true } }, grid: { hoverable: true, clickable: true }, yaxis: { min: 0, max: 60 } }); var previousPoint = null; $(".chart").bind("plothover", function (event, pos, item) { if (item) { if (previousPoint != item.dataIndex) { previousPoint = item.dataIndex; $('#tooltip').fadeOut(200, function () { $(this).remove(); }); var x = item.datapoint[0].toFixed(2), y = item.datapoint[1].toFixed(2); maruti.flot_tooltip(item.pageX, item.pageY, item.series.label + " of " + x + " = " + y); } } else { $('#tooltip').fadeOut(200, function () { $(this).remove(); }); previousPoint = null; } }); }); maruti = { flot_tooltip: function (x, y, contents) { $('<div id="tooltip">' + contents + '</div>').css({ top: y + 5, left: x + 5 }).appendTo("body").fadeIn(200); } } </script> Ultimately, I would prefer to use JSON format data and use the first value for the chart and the second for the axis. Data: [["50.00","3/18/2015 2:00:00 PM"],["37.00","3/12/2015 3:42:44 PM"],["35.00","3/11/2015 3:42:44 PM"]] Any recommendations or link to samples using this type of data would be greatly appreciated.
The string format was wrong. I ended up using Entity Framework. So my code behind looks something like: using (myEntities myEntitiesContainer = new myEntities()) { var alldata = myEntitiesContainer.myData(id).ToList(); foreach (myData i in alldata) { if (alldata.IndexOf(i) == alldata.Count - 1) { sb.Append("['").Append(i.DateData.ToString("yyyy-MM-dd HH:mmtt")).Append("', ").Append(i.SomeData.ToString()).Append("]"); } else { sb.Append("['").Append(i.DateData.ToString("yyyy-MM-dd HH:mmtt")).Append("', ").Append(i.SomeData.ToString()).Append("], "); } } myReturnString = sb.ToString(); } The return string: [['2015-03-31 16:00PM', 30.00], ['2015-03-31 14:00PM', 40.00], ['2015-03-31 13:00PM', 50.00]] The Javascript looks like: var renderGraph = function () { var plot var line1 = plot = $.jqplot('chart', [line1], { axes: { xaxis: { renderer: $.jqplot.DateAxisRenderer, tickOptions: { min: 0, formatString: '%b %#d' }, tickInterval: '1 day' }, yaxis: { tickOptions:{ formatString: '%d'} } }, highlighter: { show: true, yvalues: 1, xvalues: 1, formatString: 'Date: %s Level: %s' }, cursor: { show: false, tooltipLocation: 'sw' }, legend: { show: false } }); }
Restacking cumulative columns in Highcharts marimekko charts
I've got a basic variable width column chart (aka Marimekko) set up using Highcharts but am having trouble getting it to restack the columns properly to eliminate the data gap once a series has been removed or hidden. JSFIDDLE DEMO <-- I've set up a demo of the issue here. You'll notice clicking on a legend item removes the series from the chart, but it also removes all of the following data points in the array (i.e. clicking on series C removes series C, D, and E whereas it should redraw to A-B-D-E). Since the y-axis data is meant to display a cumulative sum of all series, these should re-shuffle as adjacent columns with no gaps. How can I get this to render properly? THIS POST uses similar demo code and attempting to solve the same problem, however the answer is somewhat elusive and I am unable to get it working. Thanks in advance! $(function () { var dataArray = [ { name: 'A', x: 200, y: 120 }, { name: 'B', x: 380, y: 101 }, { name: 'C', x: 450, y: 84 }, { name: 'D', x: 198, y: 75 }, { name: 'E', x: 95, y: 55 } ]; function makeSeries(listOfData) { var sumX = 0.0; for (var i = 0; i < listOfData.length; i++) { sumX += listOfData[i].x; } var allSeries = [] var x = 0.0; for (var i = 0; i < listOfData.length; i++) { var data = listOfData[i]; allSeries[i] = { name: data.name, data: [ [x, 0], [x, data.y], { x: x + data.x / 2.0, y: data.y, dataLabels: { enabled: false, format: data.x + ' x {y}' } }, [x + data.x, data.y], [x + data.x, 0] ], w: data.x, h: data.y }; x += data.x + 0; } return allSeries; } $('#container').highcharts({ chart: { type: 'area' }, xAxis: { tickLength: 0, labels: { enabled: true} }, yAxis: { title: { enabled: false} }, plotOptions: { series: { events: { legendItemClick: function () { var pos = this.index; var sname = this.name; var chart = $('#container').highcharts(); while(chart.series.length > 0) { chart.series[pos].remove(true); } dataArray[pos]= { name: sname, x: 0, y: 0 }; chart.series[0].setData(dataArray); } } }, area: { lineWidth: 0, marker: { enabled: false, states: { hover: { enabled: false } } } } }, series: makeSeries(dataArray) }); });
Create Line in Highcharts with start and end point
Please look at following example: <script type="text/javascript"> var $j = jQuery.noConflict(); function recalculateUTCValue(startingUTC, add) { zeit = new Date(startingUTC); zeit.setDate(zeit.getDate()+add); return zeit; } function calcDateFromUTC(utc) { d = new Date(utc); return d; } function getDaysUntilEnd(utc) { var currentTime = new Date(); var endTime = calcDateFromUTC(utc); var diff = Math.floor(( Date.parse(endTime) - Date.parse(currentTime) ) / 86400000); return diff; } </script> <script type="text/javascript"> var highchartsOptions = Highcharts.setOptions(Highcharts.theme); var TaskChart; // Chart-Objekt var container = $j('#chart01')[0]; var TaskDuration = new Array(); var startingKW = 298; // Save starting points to javascript variables for HighCharts var startingUTC = 1288087223364; // For a given time point id var startTimePoint = 0; var endTimePoint = 0; TaskDuration = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,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0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,216.0,216.0,216.0,198.0,134.0,134.0,134.0,171.0,171.0,171.0,149.0,160.5,160.5,160.5]; // Get first value which is not "0" var firstValue = 0; for(var i = 0; i < TaskDuration.length; i++) { if(TaskDuration[i] != 0) { firstValue = i; break; } } // Get largest Y-Value; need for automatically zooming (setExtremes method) var largest = Math.max.apply(Math, TaskDuration); var myStartDate; var myEndDate; // Check if we have a time point in the query if(startTimePoint != 0) { var myStartDate = calcDateFromUTC(startTimePoint); var myEndDate = calcDateFromUTC(endTimePoint); } else { // Otherwise we use the time of first created work item var myStartDate = recalculateUTCValue(startingUTC, firstValue); var myEndDate = new Date(); } </script> <script type="text/javascript"> $j(document).ready(function() { TaskChart = new Highcharts.Chart({ credits: { enabled: false }, chart: { renderTo: "chart01", defaultSeriesType: 'line', zoomType: 'x', events: { load: function(event) { this.xAxis[0].setExtremes(myStartDate, myEndDate); this.yAxis[0].setExtremes(0,largest); } } }, title: { text: "Task Burn Down Chart" }, xAxis: { type: 'datetime', dateTimeLabelFormats: { week: '%e. %b %Y' }, labels: { align: 'right', rotation: -60, x: 5, y: 15 }, offset: 10 }, yAxis: { title: { text: "Number of Hours" } }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>' + Highcharts.dateFormat('%d.%m', this.x) +': '+ this.y;; } }, plotOptions: { area: { stacking: 'normal', lineColor: '#666666', lineWidth: 1, marker: { lineWidth: 1, lineColor: '#666666' } } }, series: [ { name: 'Hours', pointStart: startingUTC, pointInterval: 24*60*60*1000, data: TaskDuration }, { type: 'line', name: 'Regression Line', data: [[myStartDate, 216], [myEndDate, 50]], marker: { enabled: false }, states: { hover: { lineWidth: 0 } }, enableMouseTracking: false }] }); }); </script> http://jsfiddle.net/JwmuT/8/ The goal is to create a Highchart line with starting point from X-Value 26th January and with the end point on the X-Value 7th February. Corresponding Y-Values are "260" and "0". How to create a simple line with these to points in HighCharts? Maybe Highcharts is able to do a linear regression on the fly?! I have found this demo but I do not know how to pass correctly X-Values in the Date format.
Highcharts doesn't calculate any type of regression or trend lines. In example you have posted, data is calculated before, and Highcharts just displays that. However there is known plugin for trendline: https://github.com/virtualstaticvoid/highcharts_trendline