Chart showing a Google Timeline: Allow for each bar to be colored differently when there are multiple bars on the same line

It appears that the Google Timeline charts have a feature to color individual blocks on the timeline as indicated in the documentation at

However, there seems to be an issue when two bars overlap on the same line, which is evident in this fiddle: http://jsfiddle.net/7A88H/21/

Below is the relevant code snippet:

dataTable.addRows([
[ 'red/green/blue', 'NAME OF BAR (should be RED) (ff0000)', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
[ 'red/green/blue', 'NAME OF BAR (should be GREEN) (00ff00)', new Date(1796, 2, 3),  new Date(1801, 2, 3) ],
[ 'red/green/blue', 'NAME OF BAR (should be BLUE) (0000ff)',  new Date(1801, 2, 3),  new Date(1809, 2, 3) ]]);

var options = {
    colors: ['#ff0000', '#00ff00', '#0000ff'],
};

I attempted to modify my data rows by adding a fifth column for the color based on the solution provided in this question: Google Charts API: Add Blank Row to Timeline?

Here is the function I tried using to build my workaround:

(function(){                                            
    var el=container.getElementsByTagName("rect");      
    var width=100000000;                                
    var elToRem=[];                                     
    for(var i=0;i<el.length;i++){                           
        var cwidth=parseInt(el[i].getAttribute("width"));
        if(cwidth<width){                             
            elToRem=[el[i]];
            width=cwidth;                               
        }
        else if(cwidth==width){                         
            elToRem.push(el[i]);        
        }
    }
    for(var i=0;i<elToRem.length;i++)
        elToRem[i].setAttribute("fill","none"); 
})();

The goal was to identify each rect element and color them accordingly based on their associated colors from the row objects. However, I struggled to retrieve the specific color information from the rect objects themselves.

Answer №1

It seems like utilizing the following additional options would be necessary:

timeline: { groupByRowLabel: false }

The reason for this is evident when visiting the g-page: . In the Bars in One Row section, they demonstrate that Presidents do not overlap, making it unsuitable for your case. However, with the method you are employing, overlapping timelines are expected to be displayed in their own row. It would also likely cause confusion if titles were to overlap.

On a side note, I observed an interesting color assignment pattern by Google where colors are assigned left to right and then wrap, while titles simply move from left to right without wrapping. To illustrate this, I created a fiddle: https://jsfiddle.net/camp185/2Lopnnt3/2/ showcasing how color wrapping functions by adding more colors.

Answer №2

function generateTimeline() {
  var container = document.getElementById('example5.4');
  var chart = new google.visualization.Timeline(container);
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn({ type: 'string', id: 'Role' });
  dataTable.addColumn({ type: 'string', id: 'Name' });
  dataTable.addColumn({ type: 'date', id: 'Start' });
  dataTable.addColumn({ type: 'date', id: 'End' });
  dataTable.addRows([
    [ 'red/green/blue', 'NAME OF BAR (should be RED) (ff0000)', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
    [ 'red/green/blue', 'NAME OF BAR (should be GREEN) (00ff00)', new Date(1796, 2, 3),  new Date(1801, 2, 3) ],
    [ 'red/green/blue', 'NAME OF BAR (should be BLUE) (0000ff)',  new Date(1801, 2, 3),  new Date(1809, 2, 3) ]]);

  var options = {
    colors: ['#ff0000', '#00ff00', '#0000ff'],
    timeline: { groupByRowLabel: false }
  };

  chart.draw(dataTable, options);
}
google.load('visualization', '1', {packages:['timeline'], callback: generateTimeline});
<script src="https://www.google.com/jsapi?.js"></script>
<div id='example5.4'></div>

Answer №3

Here is a solution that may assist you:

google.load("visualization", "1", {packages:["timeline"]});
google.setOnLoadCallback(drawChart);

// google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var container = document.getElementById('example5.4');
  var chart = new google.visualization.Timeline(container);
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn({ type: 'string', id: 'Role' });
  dataTable.addColumn({ type: 'string', id: 'Name' });
  dataTable.addColumn({ type: 'date', id: 'Start' });
  dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([
      [ 'red/green/blue', 'NAME OF BAR (should be RED) (ff0000)', new Date(1789, 3, 30), new Date(1797, 2, 4) ],
      [ 'red/green/blue', 'NAME OF BAR (should be GREEN) (00ff00)', new Date(1797, 2, 4), new Date(1801, 2, 4) ],
      [ 'red/green/blue', 'NAME OF BAR (should be BLUE) (0000ff)', new Date(1801, 2, 4), new Date(1809, 2, 4) ]

    ]);
  
 // dataTable.addRows([
 //   [ 'red/green/blue', 'NAME OF BAR (should be RED) (ff0000)', new Date(1789, 3, 29), new Date(1797, 2, 3) ],
//    [ 'red/green/blue', 'NAME OF BAR (should be GREEN) (00ff00)', new Date(1796, 2, 3),  new Date(1801, 2, 3) ],
//    [ 'red/green/blue', 'NAME OF BAR (should be BLUE) (0000ff)',  new Date(1801, 2, 3),  new Date(1809, 2, 3) ]]);

  var options = {
    colors: ['#ff0000', '#00ff00', '#0000ff'],
  };

  chart.draw(dataTable, options);

}
<script src="https://www.google.com/jsapi?.js"></script>
<div id='example5.4'></div>

Answer №4

If you're looking to change the background color of rows based on their titles, there are two methods to achieve this. In your current code, hovering over a row displays information specific to that row, but it becomes more complex as the box needs to be redrawn once the hover effect is removed. I have implemented both approaches for you:

JSFiddle with Interactivity disabled (simplified version created after the more complicated one)

JSFiddle with Interactivity enabled and messy setTimeOut functions (may not always work as intended)

Below is the code when interactivity is disabled:

function drawChart() {
   // implementation
}
google.load('visualization', '1', {packages:['timeline'], callback: drawChart});

var colorArray = []; // format:  colorArray["TEXT IN ROW"] = "COLOR"
colorArray["NAME OF BAR (should be RED) (ff0000)"] = "#ff0000";
function setColor(elem,newColor)
{
          // logic for setting new color
}

Please note that if the page width causes text in a row to be shortened by an ellipsis (...), this method won't function properly as it relies on the complete text.

-- Previous solution without workaround --

Upon further investigation, a simple fix could involve shortening the text of the green bar, as adding a new separate row doesn't resolve the issue of text overflow within the green box.

Furthermore, colors are being processed by line, causing the Blue bar to receive the green color due to the Green bar moving to a new line. It's advisable to add any rows creating a new line separately for better organization, though the order of arrays doesn't impact functionality.

View the JSFiddle here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to display an image in a React Native app using data from a JSON file?

I am currently working with JSON content that includes the path of an image. I need to display this image in my React Native application. What is the best way to render this image? {"aImages":[{"obra_path":"http:\/\/uploa ...

Ways to broaden the map of a variable in SCSS

Currently, I am utilizing a package known as Buefy, which acts as a Vue.js wrapper for the Bulma CSS framework library. Within Buefy's template components, there is an attribute/property called type (e.g., type="is-warning"). According to the document ...

React does not display the items enclosed within the map function

I am facing an issue with rendering elements from a map function. Despite trying to modify the return statement, I have not been able to resolve the issue. class API extends Component { myTop10Artists() { Api.getMyTopArtists(function (err, data) { ...

Encountering a SonarQube error message stating "Unnecessary 'undefined' should be removed" when using "undefined" as a function argument

When calling a function, I have been passing "undefined" multiple times as a parameter. However, the SonarQube report is flagging an error stating "Remove this redundant undefined". https://i.stack.imgur.com/YhOZW.png It should be noted that the function ...

Locate a specific value within an array of objects and display it

Data : const areas = [ { "ID":"4131", "RID":"f1438b", "Name":"City" }, { "ID":"6266", "RID":& ...

Customizing Semantic UI Navigation Menu Styles

For the first time, I am incorporating semantic-ui into my React application. The specific package I am working with can be found here: To display my header, I am using the following code: <Menu className='header' > <Containe ...

How do I fix the build error that says "Operator '+' cannot be used with types 'number[]'?

The function below is designed to generate unique uuidv4 strings. function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)) ...

What is the best way to transform an array of arrays into an array of objects using AngularJS?

Here is some code I am working on: $scope.students=[]; $scope.students[[object Object][object Object]] [0]{"id":"101","name":"one","marks":"67"} [1]{"id":"102","name":"two","marks":"89"} I would like to reformat it into the ...

How to create a thumbnail hover effect with CSS3 and javascript, overcoming the z-axis issue

Currently, I am working on creating a set of thumbnails that enlarge when hovered over. The initial setup achieves the zoom effect using CSS3 transform:scale and ease-in-out. However, the issue is that the enlarged images overlap each other due to sharing ...

Having difficulty managing asynchronous functions with the useState hook in React

import React from "react"; import { UserContext } from "./../contexts"; import { removeStoredAuthData, storedAuthIsValid, storeNewAuthData, } from "./../utils/auth"; import { getUserInfos } from "./../api/userAuthen ...

What are some methods to prevent cookies from being overridden?

Just beginning my journey in web development. Currently utilizing asp.net Web API and Angular with token authentication. Every time a user logs in, I set the token in a cookie and send it with each request. Everything has been running smoothly so far, bu ...

What is causing my button to act in this way?

Initially, the website redirects to an undefined URL and then to test.com. I am looking for a way to implement a redirection sequence from to and finally to <head> <script type="text/javascript"> <!-- function popup(url ...

Tips for Storing Your Data Collection: A Guide on Different Storage Options

In the different parts of my app, I require access to a dataset containing timezones. Traditionally, I have stored such data in a class method like Timezone.all_zones (Ruby) for easy accessibility anywhere in my code. Is there a way to achieve this same ...

React Native application fails to return any values from an asynchronous operation in App function

Completely new to React Native, this is my first attempt at coding. I'm struggling with returning jsx from my App() function due to an asynchronous operation within it. Here's the code that I believe clearly demonstrates my issue: import React fr ...

Invoke a separate function after a successful Ajax request

I am currently working on an AJAX call in MVC3 and here is the snippet of code I have: save: function () { $.ajax({ url: "@Url.Action("Save")", type:"post", data: ko.toJSON(this), contentType:"applic ...

What could be causing my data to appear as undefined or empty? I am using jQuery.post() to send data from JavaScript to PHP

Issue: I am encountering a problem while sending data to my PHP using jQuery's $.post method. The variable data appears to be undefined for some reason. Let me describe the structure of my code... 1. Here is the button with an onClick function: $dat ...

CSS Transition - Troubleshooting my malfunctioning code

I seem to be facing a simple issue with my code. The following snippet does not work in Chrome or Firefox, as the transition property is not working properly. Instead of smoothly transitioning from blue to red when hovered over, the element immediately swi ...

Find and target all CSS elements that have the property of "display: inline

Can the CSS be searched by property:value instead of selector/attribute? Or does this require parsing through a server script? If it is achievable, I am considering developing a script that will automatically include the IE7 hack when the selector contain ...

There seems to be a glitch in my programming that is preventing it

Can someone please help me troubleshoot this code? I'm unable to figure out what's going wrong. The concept is to take user input, assign it to a variable, and then display a string. However, nothing appears on the screen after entering a name. ...

Items in a pair of distinct columns

I want to enhance a picture by adding three paragraphs on the left-hand side. Below is my HTML code: <md-toolbar layout-align="center center"> <h3>Traffic Light Component</h3> </md-toolbar> <md-grid-list md-cols ...