Doesn't show data correctly - graph

I have prepared linear graph and some data here.
As you can see, when you try display details of samples in the middle, graph show detail of another one. As the date format I am using Unix timestamp.
Next problem is rectangle below which should show sample's date, instead of it show day, month, and some number. I require date format like YYYY/MM/DD - mm:ss.
var chart = AmCharts.makeChart( "chartdiv", {
"type": "serial",
"theme": "light",
"marginRight": 80,
"autoMarginOffset": 20,
"marginTop": 7,
"dataDateFormat": "YYYY/MM/DD JJ:NN:QQQ",
"dataProvider": chartData,
"valueAxes": [{
"axisAlpha": 0.2,
"dashLength": 1,
"position": "left",
}],
"mouseWheelZoomEnabled": true,
"graphs": [{
"id": "g1",
"balloonText": "BallonText",
"bullet": "round",
"bulletBorderAlpha": 1,
"bulletColor": "#FFFFFF",
"hideBulletsCount": 50,
"title": "red line",
"valueField": "yCoordinate",
"useLineColorForBulletBorder": true,
"balloon":{
"drop":true
}
}],
"chartScrollbar": {
"autoGridCount": true,
"graph": "g1",
"scrollbarHeight": 40
},
"chartCursor": {
"limitToGraph":"g1"
},
"categoryField": "xCoordinate",
"categoryAxis": {
"parseDates": true,
"axisColor": "#DADADA",
"dashLength": 1,
"minorGridEnabled": true
},
"export": {
"enabled": true
},
} );

There are a couple of issues.
1) Your date-based data must be sorted in ascending, per the parseDates documentation documentation. Your dates are out of order, which will cause chart behavior issues like what you're seeing.
2) You have to set your category axis minPeriod to match the smallest period between each of your dates in your data. It looks like seconds ("ss") are appropriate.
As for formatting the chart cursor, you can set categoryBalloonDateFormat to the desired format. In this case "YYYY/MM/DD - NN:SS" is what you want. Refer to the formatting dates documentation if you need to use different formats.
Also note that dataDateFormat is not necessary if you're using millisecond timestamps. dataDateFormat is only used to parse your date data if they are strings.
Updated code below:
var chartData = [
{
xCoordinate: 1511509736056,
yCoordinate: 1
},
{
xCoordinate: 1511509955035,
yCoordinate: 1
},
{
xCoordinate: 1511510013033,
yCoordinate: 1
},
{
xCoordinate: 1511510152052,
yCoordinate: 1
},
{
xCoordinate: 1511510436036,
yCoordinate: 1
},
{
xCoordinate: 1511510664024,
yCoordinate: 1
}
];
//sort dates into ascending order
chartData.sort(function(lhs, rhs) {
return lhs.xCoordinate - rhs.xCoordinate;
});
var chart = AmCharts.makeChart("chartdiv", {
type: "serial",
theme: "light",
marginRight: 80,
autoMarginOffset: 20,
marginTop: 7,
dataProvider: chartData,
valueAxes: [
{
axisAlpha: 0.2,
dashLength: 1,
position: "left"
}
],
mouseWheelZoomEnabled: true,
graphs: [
{
id: "g1",
balloonText: "BallonText",
bullet: "round",
bulletBorderAlpha: 1,
bulletColor: "#FFFFFF",
hideBulletsCount: 50,
title: "red line",
valueField: "yCoordinate",
useLineColorForBulletBorder: true,
balloon: {
drop: true
}
}
],
chartScrollbar: {
autoGridCount: true,
graph: "g1",
scrollbarHeight: 40
},
chartCursor: {
limitToGraph: "g1",
categoryBalloonDateFormat: "YYYY/MM/DD - NN:SS" //change date format in cursor
},
categoryField: "xCoordinate",
categoryAxis: {
parseDates: true,
axisColor: "#DADADA",
dashLength: 1,
minPeriod: "ss", //update min period to match the smallest intervals in your data.
minorGridEnabled: true
},
export: {
enabled: true
}
});
html, body {
width: 100%;
height: 100%;
margin: 0px;
}
#chartdiv {
width: 100%;
height: 100%;
}
<script src="//www.amcharts.com/lib/3/amcharts.js"></script>
<script src="//www.amcharts.com/lib/3/serial.js"></script>
<script src="//www.amcharts.com/lib/3/themes/light.js"></script>
<script src="//www.amcharts.com/lib/3/amstock.js"></script>
<div id="chartdiv"></div>

Related

Datatables - Adding css to specific cells

I am populating and displaying my data using Datatables. I am attemping to add css to the background of a specific cell under my Revenue Growth Column if a certain condition is met.
For example: if revenue growth column is less then 3 then I would like to make that cell background-color: red
Here is the array I am using to populate my table :
const data = [
{title: "Walk in", totalRevenue: 2002, growth: 3.2},
{title: "Retail", totalRevenue: 1231, growth: 2.2},
{title: "Hospital", totalRevenue: 5421, growth: 1.9},
{title: "Online", totalRevenue: 2442, growth: 3.2},
{title: "Fitness", totalRevenue: 8742, growth: 0.3}
]
I've attempted this by using
rowCallback: function(row, data, index){
if(data[2] < 3){
$(row).find('td:eq(2)').css('background-color', 'red');
}
Which I believe is checking column 3 which would be the value of my growth in my array. Currently with this line of code my data table has not changed.
My expected outcome is to have the background display red for any of the values that is less then 3.
Here is a link to a jsfiddle for an example of what I am working with:
The data parameter in your rowCallback is an object.
if (data.growth < 3) {
$(row).find('td:eq(2)').css('background-color', 'red');
}
Just need to change the if condition from data[2] to data.growth
$(document).ready(function() {
let className = ""
const data = [{
title: "Walk in",
totalRevenue: 2002,
growth: 3.2
}, {
title: "Retail",
totalRevenue: 1231,
growth: 2.2
},
{
title: "Hospital",
totalRevenue: 5421,
growth: 1.9
},
{
title: "Online",
totalRevenue: 2442,
growth: 3.2
},
{
title: "Fitness",
totalRevenue: 8742,
growth: 0.3
}
]
var table = $('#example').DataTable({
rowCallback: function(row, data, index) {
console.log({
data
})
if (data.growth < 3) {
$(row).find('td:eq(2)').css('background-color', 'red');
}
},
"columnDefs": [{
"targets": [1, 2],
"className": 'dt-body-right'
}, ],
data: data,
responsive: true,
paging: true,
searching: false,
bInfo: true,
"order": [
[2, "desc"]
],
"pageLength": 20,
columns: [{
data: "title",
title: "Title",
},
{
data: "totalRevenue",
title: 'Revenue'
},
{
data: "growth",
title: 'Revenue Growth'
},
]
});
});
<link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" style="width:100%">
</table>

how can i show my custom (Day-Month-Year Hour:Min:Sec) time format on chartjs chart?

I want to show my custom (Day-Month-Year Hour:Min:Sec -->ex: 01-05-2019 14:06:47 PM) time format on chartjs chart
How Can i Show On chart xAxis Date Format Like This >>
Day-Month-Year Hour:Min:Sec -->ex: 01-05-2019 14:06:47 PM
time format is timeFormat = 'DD/MM/YYYY h:mm:ss a' but on chart only shows Month,Day,Year
This is my code below and:
Online Code On >>> https://codepen.io/sibche2013/pen/XQWWbb
var timeFormat = 'DD/MM/YYYY h:mm:ss a';
var config = {
type: 'line',
data: {
datasets: [
{
label: "UK Dates",
data: [{
x: "01-04-2014 02:15:50", y: 175
}, {
x: "12-04-2014 12:19:27", y: 177
}, {
x: "23-04-2014 22:25:47", y: 178
}, {
x: "28-04-2014 14:46:40", y: 182
}],
fill: false,
borderColor: 'blue'
}
]
},
options: {
responsive: true,
title: {
display: true,
text: "Chart.js Time Scale"
},
scales: {
xAxes: [{
type: "time",
time: {
format: timeFormat,
tooltipFormat: 'll'
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'value'
}
}]
}
}
};
window.onload = function () {
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx, config);
};

Highcharts: displaying datetime like categories

I'm trying to achieve something like this: example.
I want to display labels for xAxis like in categories: in Month day-day format. Is it possible to achieve it with 'datetime' type?
This is my xAxis configuration:
{
"type" : "datetime",
"crosshair" : false,
"visible" : true,
"labels" : {
"enabled" : true,
"padding" : 10
},
"minTickInterval" : 86400000,
"tickLength" : 10,
"min" : 1507759200000,
"max" : 1523311199999
}
You can try setting xAxis.labels.x offset when xAxis.showLastLabel is disabled:
var chart = Highcharts.chart('container', {
chart: {
width: 700
},
xAxis: {
type: 'datetime',
labels: {
x: 150
},
showLastLabel: false
},
series: [{
data: [
[Date.UTC(2018, 0), 1],
[Date.UTC(2018, 1), 2],
[Date.UTC(2018, 2), 2]
]
}]
});
Live demo: http://jsfiddle.net/BlackLabel/0bk79bpz/
API references:
https://api.highcharts.com/highcharts/xAxis.labels.x
https://api.highcharts.com/highcharts/xAxis.showLastLabel

Hierarchical Layout : nodes not created as per insertion order in same level

I have assigned level to each node. Now on each level, I want nodes to appear in same order in which it is inserted. That's what even documentation says. I have seen many examples where it happens and only difference with my case is: there are edges on X axis too.
Here's a snippet:
function main() {
var graph = {
nodes: new vis.DataSet([
{ "id": "M1", "label": "M1", "level": 0 },
{ "id": "R1", "label": "R1", "level": 0 },
{ "id": "W1", "label": "W1", "level": 0 },
{ "id": "C1R1", "label": "C1R1", "level": 1 },
{ "id": "C2R1", "label": "C2R1", "level": 1 },
{ "id": "R2R1", "label": "R2R1", "level": 1 },
{ "id": "W2R1", "label": "W2R1", "level": 1 },
{ "id": "C3R1", "label": "C3R1", "level": 1 }
]),
edges: new vis.DataSet([
{ "from": "M1", "to": "R1" },
{ "from": "W1", "to": "R1" },
{ "from": "M2", "to": "R2" },
{ "from": "W2", "to": "R2" },
{ "from": "R1", "to": "C1R1" },
{ "from": "R1", "to": "C2R1" },
{ "from": "C2R1", "to": "R2R1" },
{ "from": "W2R1", "to": "R2R1" },
{ "from": "R1", "to": "C3R1" }
])
};
var options = {
nodes: {
borderWidth: 1,
borderWidthSelected: 1,
shape: "box",
color: {
border: 'lightgray',
background: 'white',
highlight: {
border: 'lightgray',
background: 'lightblue'
},
hover: {
border: 'lightgray',
background: 'lightblue'
}
}
},
edges: {
smooth: {
type: 'cubicBezier',
forceDirection: 'vertical',
roundness: 1
},
color: 'lightgray'
},
layout: {
hierarchical: {
direction: 'UD',
nodeSpacing: 150
}
},
interaction: {
dragNodes: true
},
physics: false,
edgeMinimization: false,
blockShifting: false
};
var network = new vis.Network(document.getElementById("network"), graph, options);
}
#network {
width: 100%;
height: 180px;
}
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.16.1/vis.min.css" />
</head>
<body onload="main();">
<div id="network"></div>
</body>
The main bit is:
var nodes = new vis.DataSet([
{"id": "M1", "label": "M1", "level": 0},
{"id": "R1", "label": "R1", "level": 0},
{"id": "W1", "label": "W1", "level": 0},
{"id": "C1R1", "label": "C1R1", "level": 1},
{"id": "C2R1", "label": "C2R1", "level": 1},
{"id": "R2R1", "label": "R2R1", "level": 1},
{"id": "W2R1", "label": "W2R1", "level": 1},
{"id": "C3R1", "label": "C3R1", "level": 1}
]);
It's a family tree so I want that husband, wife and marriage node stay together.
This is what I am getting:
This is what I am looking for:
Basically on X axis, nodes should be shown in same order as they are inserted (thus no crossing or overlapping).
I have tried by keeping edge minimization as false, blockShifting as false. Tried even by giving x position, but it will still adjust itself.
Do let me know if there is any way to get it or there is no way.
I finally decided to solve it by calculating positions of each node and fixing it.
Decide shape and corresponding size, spacing required by each node.
Level is already defined so that will give you 'y' co-ordinate of each node.
And for 'x' co-ordinate, say each node require space of 30 px and you want to keep margin of 5px on each side, so total 40 px.
At each level, for each node, you calculate how much x space (width) is required to draw all its child node and go on recursively.
In above example, R1 would need 200 (40*5) px width for x, so M1 and W1 can be drawn after or before that only. R1 can be drawn at center of 200 px width and we can get positions for C1R1, C2R1,R2R2, W2R1 and C3R1 by utilizing 40px for each.
If we start drawing at let's say: -200, 0
Then
M1 => x : -180, y : 0 (occupying space from -200 to -160)
R1 => x : -60, y : 0 (occupying space from -160 to 40)
W1 => x : 60, y: 0 (occupying space from 40 to 80)
C1R1 => -140, y: 40 (occupying space from -160 to -120)
C2R1 => -100, y: 40 (occupying space from -120 to -80)
R2R2 => -60, y: 40 (occupying space from -80 to -40)
W2R1 => -20, y: 40 (occupying space from -40 to 0)
C3R1 => 20, y: 40 (occupying space from 0 to 40)
Let me know if you want me to write exact code. It was very different for me, as I had different sizes of node and some other relationships etc

CSS Color table based on input in each row

Would it be possible to color each table rows based on a input in a specific column for each row?
Example, if:
B1 = 1 // Red row
B2 = 1 // Red row
B3 = 3 // Blue row
B4 = 2 // Green row
B5 = 1 // Red row
And so on?
It's a datatable and there will automatically be filled new rows into the table, these should also be coloured after the system.
Demo
var dataSet = [['Dadar', 'lmsSenitaD', 'Atul salaskar', '9876543210', '', 'Not Joined', '10/01/2014', '', 'Come back and Join', 'Mobile', 'Times','1'],
['Aundh', 'Rashmi', 'Preeti Gupta', '9876543210', '', 'Not Joined', '10/01/2014', '', 'Will Discuss with Family', 'Online Campaign', 'Iksula','2'],
['Do#Home_Thane', 'Rashmi', 'Mitali Gupta', '9876543210', '', 'Joined - Old Date', '10/01/2014', '20/08/2014', 'Come back and Join', 'Online Campaign', 'Iksula','4']];
$(document).ready(function () {
$('#demo').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>');
$('#example').dataTable({
"data": dataSet,
"columns": [
{ "title": "Center" },
{ "title": "Call Executive" },
{ "title": "Name" },
{ "title": "Mobile" },
{ "title": "Phone" },
{ "title": "Status" },
{ "title": "Appt Date" },
{ "title": "Joined Date" },
{ "title": "Remark" },
{ "title": "Source" },
{ "title": "Publisher" },
{ "title": "css" },
]
,
"fnRowCallback": function (nRow, aData, iDisplayIndex) {
var css = aData[aData.length - 1];
if (css == "1") {
$(nRow).addClass('gradeN');
}
else if(css == "2") {
$(nRow).addClass('gradeC');
}
else{
$(nRow).addClass('gradeM');
}
}

Resources