my graph get data from server, time server * 1000 for convert to time js. And here my graph
http://awesomescreenshot.com/03f1ft5k72
The poin not match the column day. I must + 25 000 000 to each value to show it match column day. Why this happen,I'm new with flot. Can you share me your experence.
var d_register = #{ #total.map{ |t| [t.date.to_time.to_i * 1000 , t.register] } };
// must add 25 000 000 to it for correct position point.
// var d_register = #{ #total.map{ |t| [t.date.to_time.to_i * 1000 + 25000000, t.register] } };
var curr = new Date(#{Date.current.to_time.to_i * 1000}); // get current date
define_flot();
function define_flot(){
var first = new Date(curr.getTime() - curr.getDay() * 24 * 60 * 60 * 1000);
var last = new Date(first.getTime() + 7 * 24 * 60 * 60 * 1000);
$(".week-time").text(
(first.getMonth() + 1) + '/' +
first.getDate() + '/' +
first.getFullYear() + ' - ' +
(last.getMonth() + 1) + '/' +
last.getDate() + '/' +
last.getFullYear() );
var first_day_in_week = first.getTime();
var last_day_in_week = last.getTime();
$.plot("#placeholder", [ d_register ], {
yaxis: {
tickDecimals: 0
},
xaxis: {
mode: "time",
minTickSize: [1, "day"],
min: first_day_in_week,
max: last_day_in_week,
timeformat: "%a"
}
});
}
data_register :
[[1370710800000, 1], [1370797200000, 7], [1370883600000, 1], [1371056400000, 0], [1371142800000, 0], [1371747600000, 0], [1371834000000, 0], [1371920400000, 0], [1372006800000, 0], [1372093200000, 0], [1372179600000, 0]]
data_register after + 25000000:
[[1370735800000, 1], [1370822200000, 7], [1370908600000, 1], [1371081400000, 0], [1371167800000, 0], [1371772600000, 0], [1371859000000, 0], [1371945400000, 0], [1372031800000, 0], [1372118200000, 0], [1372204600000, 0]]
Your screenshot link appears to have broken, but as I recall the problem was that the values were supposed to be aligned to the beginning of a day, but were showing offset by a few hours.
The problem is that Flot expects UTC timestamps, while yours are offset. 1370710800000, for example, is actually June 8, 2013 at 17:00 GMT. By adding 25M - 1370735800000 - that becomes 23:56:40, which is close enough that it appears to line up to the day boundary. Really what you want to add is 7 (hours) * 3600 (seconds) * 1000 (milliseconds) = 25200000, which compensates for what I'm assuming is your local timezone offset of GMT-7, US Pacific Time.
Related
sample input: 3 1 4 2
output: 1) Subarrays of size 1 : (3),(1),(4),(2) , sum = 0 + 0 + 0 + 0 = 0.
2) Subarrays of size 2: [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7.
3) Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6.
4) Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16
Here the problem can solve in many ways, hope this might help you to get your ideas.
arr = [3, 1, 4, 2]
lists = []
res = 0
for i in range(len(arr) + 1):
for j in range(i):
if len(arr[j: i]) > 1:
res += np.max(arr[j:i]) - np.min(arr[j:i])
lists.append(arr[j:i])
print(res)
I have this chart
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="height: 300px"></div>
var seriesOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL', 'GOOG'];
/**
* Create the chart when all data is loaded
* #returns {undefined}
*/
function createChart() {
Highcharts.stockChart('container', {
plotOptions: {
series: {
gapSize: 5 * 24 * 3600 * 1000,
gapUnit: 'relative'
}
},
rangeSelector: {
selected: 5
},
yAxis: {
labels: {
formatter: function () {
return (this.value > 0 ? ' + ' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},
plotOptions: {
series: {
compare: 'percent',
showInNavigator: true
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2,
split: true
},
series: seriesOptions
});
}
$.each(names, function (i, name) {
$.getJSON('https://www.highcharts.com/samples/data/' + name.toLowerCase() + '-c.json', function (data) {
if (i==0) {
var first = [], last = [];
first.push.apply(first, data.slice(0,1)[0]);
last.push.apply(first, data.slice(0,1)[0]);
first[0] = first[0] - 1900 * 24 * 3600 * 1000;
last[0] = last[0] - 130 * 24 * 3600 * 1000;
data = [];
data.push(first);
data.push(last);
}
seriesOptions[i] = {
name: name,
data: data
};
// As we're loading the data asynchronously, we don't know what order it will arrive. So
// we keep a counter and create the chart when all the data is loaded.
seriesCounter += 1;
if (seriesCounter === names.length) {
createChart();
}
});
});
and as you can see there are three stocks shown. If you hover the chart with the mouse and go to the beginning you'll notice MSFT stock which has only 2 points and that's intentional. After MSFT there should be about 6 year gap, however on the chart it's shown in a few pixels.
How can I configure stockChart to show real gaps? In other words, I want to see the gap of 6 years so from 2005 till 2011 there will be empty space proportional to the whole chart?
The discussion in the comment section of the first answer reveals that OP wants to hide no-data periods only in some cases.
The solution here might be to set ordinal to false (as #ewolden) suggested and use breaks instead:
xAxis: {
breaks: [{
breakSize: 24 * 3600 * 1000,
from: Date.UTC(2017, 0, 6),
to: Date.UTC(2017, 0, 9)
}],
ordinal: false
},
series: [{
data: [
[Date.UTC(2017, 0, 2), 6],
[Date.UTC(2017, 0, 3), 7],
[Date.UTC(2017, 0, 4), 3],
[Date.UTC(2017, 0, 5), 4],
[Date.UTC(2017, 0, 6), 1],
[Date.UTC(2017, 0, 9), 8],
[Date.UTC(2017, 0, 10), 9],
[Date.UTC(2017, 6, 1), 4],
[Date.UTC(2017, 6, 2), 5]
]
Example: http://jsfiddle.net/BlackLabel/ocg0dujg/
In the above demo I was able to hide the weekend (7 and 8 Jan) and maintain the space between January and July.
API reference: https://api.highcharts.com/highstock/xAxis.breaks
What you are after is ordinal.
In an ordinal axis, the points are equally spaced in the chart regardless of the actual time or x distance between them. This means that missing data for nights or weekends will not take up space in the chart.
Setting ordinal to false, like this, will give you the gap you are after:
xAxis: {
type: 'datetime',
ordinal: false,
},
There are some other issues with your code, if you look in console, you are getting error 15 which states that highcharts requires data to be sorted. You get this because of how you add the series data to your MSFT series. You add both the x and the y to a single 1D array, which means highcharts tries to plot both your x and y values on the x axis.
I did a workaround that gives it the right format in this fiddle: http://jsfiddle.net/2cps91ka/91/
DST hour 2:00 to 3:00 is not rending fine on highchart. I have below data.
[new Date('2017/03/12 01:00:00').getTime(), 10],
[new Date('2017/03/12 02:00:00').getTime(), 20],
[new Date('2017/03/12 03:00:00').getTime(), 30],
[new Date('2017/03/12 04:00:00').getTime(), 40],
[new Date('2017/03/12 05:00:00').getTime(), 50]
Check fiddle
http://jsfiddle.net/SurenderK/ko8dk2fd/4/
I found a workaround for this. Issue was with the ordinate we are sending. as new Date() is covering 2:00 and 3:00 to the same time for DST. So we need to send right ordinates in milliseconds itself.
I used a function convertInMill that will convert time string to UTC and add timezone offset back.
var convertInMill = function(stringDate) {
var obj, year, mnth, day, hr, min, sec, timeZoneOffset, timeInMillS;
timeZoneOffset = 1; // machine timezone offset with UTC timezone
obj = stringDate.split(/[\s/:-]+/);
year = obj[0];
mnth = obj[1];
day = obj[2];
hr = obj[3];
min = obj[4];
sec = obj[5];
timeInMillS = Date.UTC(year, mnth - 1, day, hr, min, sec) + (timeZoneOffset * 3600000);
return timeInMillS;
}
Series date
series: [{
data: [
[convertInMill('2017/03/12 00:00:00'), 10],
[convertInMill('2017/03/12 01:00:00'), 20],
[convertInMill('2017/03/12 02:00:00'), 30],
[convertInMill('2017/03/12 03:00:00'), 40],
[convertInMill('2017/03/12 04:00:00'), 50]
]
}]``
Below fiddle is working fine for me.
http://jsfiddle.net/SurenderK/ko8dk2fd/11/
In Swift 3, how can we calculate sum of every 5 elements in array of Int.
For example, we have an array [1,2,3,4,5,6,7,8,9,0,12,23]
1+2+3+4+5 = 15
6+7+8+9+0 = 30
12+23+0+0+0 = 35
The result something like this [15,30,35]
Here is my solution in playgroud:
//: Playground - noun: a place where people can play
import UIKit
var arr = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3]
let chunkSize = 5
let chunks = stride(from: 0, to: arr.count, by: chunkSize).map {
Array(arr[$0..<min($0 + chunkSize, arr.count)])
}
print(chunks)
var summ = chunks.map { $0.reduce(0, {$0 + $1}) }
print(summ)
OUTPUT:
[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3]]
[5, 10, 15]
Take a look at:
Finding sum of elements in Swift array
How do I convert such technique (from nVidia InstancedTesselation sample) to plain HLSL + DirectX 11 C++ code?
float4 PreprocessedLoDVS( uint id : SV_InstanceID, uniform int method) : LODS
{
float4 tessLevel;
if (method == 1) //Gregory
{
float3 positionControlPoints[20];
// 8 9 10 11
// 12 0\1 2/3 13
// 14 4/5 6\7 15
// 16 17 18 19
LoadGregoryPositionControlPoints(id, positionControlPoints);
tessLevel.x = evaluateEdgeLoD(positionControlPoints[16], positionControlPoints[14], positionControlPoints[12], positionControlPoints[8]);
tessLevel.y = evaluateEdgeLoD(positionControlPoints[19], positionControlPoints[15], positionControlPoints[13], positionControlPoints[11]);
tessLevel.z = evaluateEdgeLoD(positionControlPoints[16], positionControlPoints[17], positionControlPoints[18], positionControlPoints[19]);
tessLevel.w = evaluateEdgeLoD(positionControlPoints[8], positionControlPoints[9], positionControlPoints[10], positionControlPoints[11]);
}
else if (method == 0) { //Regular
float3 positionControlPoints[16];
// 0 1 2 3
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
LoadRegularControlPoints(id, positionControlPoints);
tessLevel.x = evaluateEdgeLoD(positionControlPoints[ 0], positionControlPoints[ 4], positionControlPoints[ 8], positionControlPoints[12]);
tessLevel.y = evaluateEdgeLoD(positionControlPoints[ 3], positionControlPoints[ 7], positionControlPoints[11], positionControlPoints[15]);
tessLevel.w = evaluateEdgeLoD(positionControlPoints[ 0], positionControlPoints[ 1], positionControlPoints[ 2], positionControlPoints[ 3]);
tessLevel.z = evaluateEdgeLoD(positionControlPoints[12], positionControlPoints[13], positionControlPoints[14], positionControlPoints[15]);
}
else if (method == 2) { //Bezier
float3 positionControlPoints[16];
// 0 1 2 3
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
LoadBezierPositionControlPoints(id, positionControlPoints);
tessLevel.x = evaluateEdgeLoD(positionControlPoints[ 0], positionControlPoints[ 4], positionControlPoints[ 8], positionControlPoints[12]);
tessLevel.y = evaluateEdgeLoD(positionControlPoints[ 3], positionControlPoints[ 7], positionControlPoints[11], positionControlPoints[15]);
tessLevel.z = evaluateEdgeLoD(positionControlPoints[12], positionControlPoints[13], positionControlPoints[14], positionControlPoints[15]);
tessLevel.w = evaluateEdgeLoD(positionControlPoints[ 0], positionControlPoints[ 1], positionControlPoints[ 2], positionControlPoints[ 3]);
}
else if (method == 3) { //Pm
// 18 14 13 12
// 19 8
// 20 7
// 0 1 2 6
float3 positionControlPoints[24];
LoadPmControlPoints(id, positionControlPoints);
tessLevel.x = evaluateEdgeLoD(positionControlPoints[ 0], positionControlPoints[20], positionControlPoints[19], positionControlPoints[18]);
tessLevel.y = evaluateEdgeLoD(positionControlPoints[ 6], positionControlPoints[ 7], positionControlPoints[ 8], positionControlPoints[12]);
tessLevel.z = evaluateEdgeLoD(positionControlPoints[18], positionControlPoints[14], positionControlPoints[13], positionControlPoints[12]);
tessLevel.w = evaluateEdgeLoD(positionControlPoints[ 0], positionControlPoints[ 1], positionControlPoints[ 2], positionControlPoints[ 6]);
}
else {
tessLevel=float4(2,2,2,2);
}
return tessLevel;
}
technique10 LoDRegularTechnique
{
pass P0
{
SetDepthStencilState( DisableDepthWrites, 0 );
SetVertexShader( CompileShader( vs_4_0, PreprocessedLoDVS(0) ) );
SetGeometryShader( ConstructGSWithSO( CompileShader( vs_4_0, PreprocessedLoDVS(0) ), "LODS.xyzw" ) );
SetPixelShader( NULL );
}
}
PreprocessedLoDVS looks like usual vertex shader, except "LODS" signature, and what about geometry shader?
This link explains it pretty nicely, but basically to summarize.
Compile your shader to blob, using PreprocessedLoDVS and vs_4_0 as profile
Create an Stream output layout, which is an array of D3D11_SO_DECLARATION_ENTRY , that will be LODS semantic with 4 components.
Create your vertex shader using CreateGeometryShaderWithStreamOut
Create a buffer with D3D11_BIND_STREAM_OUTPUT flag (same as you would create any other buffer, from that sample you will need also Shader Resource Flag, since you will need SRV to bind back as Buffer input).
Bind this buffer to stream output (using SOSetTargets)
Set your Vertex Shader to the pipeline and do your draw call.