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>
Related
I've the following date: "2022-06-04T00:00:00.000Z" format, We've need of date calculation for highcharts in this numeric type format "1525844100000" so how can I calculate in react-highcharts
xAxis: {
type: 'datetime',
ordinal: false,
startOnTick: false,
endOnTick: false,
minPadding: 0,
maxPadding: 0,
Date: false,
tickInterval: 4 3600 1000,
minRange: 1 24 3600000,
dateTimeLabelFormats: {
day: '%l %P',
hour: '%l %P'
},
offset: 0,
},
series: [
{
"data": [[1.424304e+12, 0.25]],
color: '#FFA749',
},
],
"highcharts": "^6.1.1",
"react-highcharts": "^16.0.2",
I've given above my charts details
In order to convert ISO-8601 to timestamp format, use getTime() JS method.
let timestamp = new Date('2022-06-04T00:00:00.000Z').getTime()
// expected output: 1654300800000
Demo: https://jsfiddle.net/BlackLabel/vnLdum1k/
I am trying to create error bars similar to the one in Vega Lite
Vega Lite Error Bar.
I would like to plot Q25, Q50, Q75 versus gender (as the first category) and age as the second.
I tried to organize my data in different ways, so Q is the same as Q25, Q50, Q75. I am having trouble achieving my goal.
// Generate data.
data = [
[
'jobId',
'Region',
'rid',
'ContributionLimit',
'Q25',
'Q50',
'Q75',
'entryNR',
'Gender',
'AgeCategory',
'Q'
],
[
2446,
'Baden-W\u00fcrttemberg',
9,
6900.0,
2330.0,
2628.0,
3032.0,
883,
'Gesamt',
'25 bis unter 55',
[2330.0, 2628.0, 3032.0]
],
[
2446,
'Baden-W\u00fcrttemberg',
9,
6900.0,
2330.0,
2628.0,
3032.0,
883,
'Gesamt',
'Gesamt',
[2330.0, 2628.0, 3032.0]
],
[
2446,
'Baden-W\u00fcrttemberg',
9,
6900.0,
2302.0,
2561.0,
2852.0,
777,
'Frauen',
'Gesamt',
[2302.0, 2561.0, 2852.0]
],
[
2446,
'Baden-W\u00fcrttemberg',
9,
6900.0,
2286.0,
2542.0,
2827.0,
528,
'Frauen',
'25 bis unter 55',
[2286.0, 2542.0, 2827.0]
]
];
option = {
dataset: [
{
id: 'raw',
source: data
},
{
id: 'rid_f',
fromDatasetId: 'raw',
transform: [
{
type: 'filter',
config: {
dimension: 'rid',
eq: 9
}
}
]
},
{
id: 'Gesamt',
fromDatasetId: 'rid_f',
transform: [
{
type: 'filter',
config: {
dimension: 'AgeCategory',
eq: 'Gesamt'
}
}
]
},
{
id: '25 bis unter 55',
fromDatasetId: 'rid_f',
transform: [
{
type: 'filter',
config: {
dimension: 'AgeCategory',
eq: '25 bis unter 55'
}
}
]
},
],
legend: {
top: '10%'
},
yAxis: {
type: 'category',
},
xAxis: {
type: 'value'
},
series: [
{
name: 'ab 55',
type: 'line',
datasetId: 'Gesamt',
symbolSize: 6,
encode: {
x: ['Q25', 'Q50', 'Q75'],
y: 'Gender',
label: 'Gender',
itemName: 'Gender'
}
},
{
name: '25 bis unter 55',
type: 'line',
datasetId: '25 bis unter 55',
symbolSize: 6,
encode: {
x: 'Q',
y: 'Gender',
label: 'Gender',
itemName: 'Gender'
}
}
]
};
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);
};
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>
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');
}
}