Tips for modifying the data content color of a vis.js timeline

In my Vue.js application, I am utilizing the vis.js timeline to display data.

I have incorporated the vis timeline with randomly generated data.

Here is the code snippet:

created() {
    var now = moment().minutes(0).seconds(0).milliseconds(0);
    var groupCount = 3;
    var itemCount = 20;

    var names = ['John', 'Alston', 'Lee', 'Grant'];
    for (var g = 0; g < groupCount; g++) {
      this.groups.push({
        id: g,
        content: names[g]
      });
    }

    for (var i = 0; i < itemCount; i++) {
      var start = now.clone().add(Math.random() * 200, 'hours');
      var group = Math.floor(Math.random() * groupCount);
      this.items.push({
        id: i,
        group: group,
        content: 'item ' + i +
          ' <span class="" style="color:green;">(' + names[group] + ')</span>',
        start: start,
        type: 'box',
        className: 'green', 
        title:'Testing tool tip index' + i     
      });
    }
   },

Then, I used the following code to display the timeline in my Vue component:

<timeline ref="timeline"
    :items="items"
    :groups="groups"
    :options="options"
    >
    </timeline>

This results in something like this:

https://i.sstatic.net/wuC3w.png

Please note that I have applied custom CSS changes to modify the appearance of the timeline.

As a beginner working with vis.js, I am interested in learning how to change the colors of the bars...

https://i.sstatic.net/V1IbN.png

How can I assign random colors to the bars instead of just green...

Answer №1

The most effective method I discovered for accomplishing this task was by utilizing the item's className property. For instance, within your 'create a dataset with items' loop, you could randomly assign classes like 'colour_1', 'colour_2', and 'colour_3'.

Subsequently, create a corresponding stylesheet that resembles the following:

    .colour_1 {background-color:#7fc97f; border-color:#7fc97f}
    .colour_2 {background-color:#beaed4; border-color:#beaed4}
    .colour_3 {background-color:#fdc086; border-color:#fdc086}

This approach can be further expanded by applying additional classes to the container in order to toggle between different sets of colors based on your preferences.

Shown below is a snapshot of my timeline featuring color variations implemented using this particular technique.

https://i.sstatic.net/9ZnS8.png

Alternatively, you may opt to utilize the 'template' function override to achieve a similar outcome. However, I observed that invoking the 'redraw' function noticeably slowed down the process compared to simply switching between color schemes via CSS.

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

Animating the three.js orbital controls to reset them

Is there a method to adjust the angle of orbital controls in three.js? Whenever I attempt to set the azimuthal and polar angles using controls.setPolarAngle(myangle), an error is displayed saying controls.setPolarAngle is not a function. controls.setPolar ...

Categorize elements in an array based on a specific keyword

Struggling with my AngularJS (1) application, I can't seem to figure out how to split an array of items into separate arrays grouped by item. In simpler terms, I have an array with different items and I want to group them by their uuid like this: [ ...

Identify which anchor tag from the group with the matching class was selected and retrieve its unique identifier

There are multiple anchor tags with the same class in my code. <a href='' id='id1' class='abc'>Link 1</a> <a href='' id='id2' class='abc'>Link 2</a> <a href='&apos ...

How to effectively expose a node controller function for testing in Mocha

Below is the code for my foo-controller.js. module.exports = function(params) { var router = require('express').Router(); var db = params.db var controller = {}; controller.getFoo = function (req, res) { // logic to ret ...

Creating a dynamic dropdown in a Rails application using the simple_form gem

In my Rails application, I have a straightforward has_many and belongs_to relationship. Using simple_form, I am looking to dynamically adjust the dropdown options based on the selection made by the user. Models class Processor < ApplicationRecord ...

Integrating Nuxt.js: utilizing both transition events and enter/leave events for seamless animations

Currently, I have a code snippet that handles transitions between pages effectively: export default { transition: { mode: 'out-in', css: false, beforeEnter (el) { console.log('set transition'); }, enter (el, ...

Encountering a cross-origin resource sharing (CORS) error while attempting

My Vue App is being hosted on an express server (nodejs running on port 60702) with the following setup: 'use strict'; const fs = require('fs'); const path = require('path'); const express = require('express'); var h ...

What could be causing the issue with the unexpected token in the API Ajax request?

I have a document that contains the following content: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> </head> <body> <script src="http://ajax.aspnetcdn.com/ ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...

Alert all simultaneous elements of modifications

I am attempting to update parallel components (which are rendered with v-for) when changes occur. Here is the code snippet I am using: <div v-for="item in items" :key="item.ID"> <my-component :item="item"></ ...

It's quite baffling: How come the arrangement of my objects becomes disorganized following an ajax request even though I haven't made any modifications?

I am experiencing an issue where the objects retrieved from two nested ajax calls to the server are not in ascending order despite being ordered correctly by the server. The first ajax call successfully returns XML data with objects sorted by date in ascen ...

Why aren't my laptop media query CSS styles being applied?

My laptop size media query is not applying the CSS styles I want. I have tried using both min and max width but without success. I am trying to replicate the layout found here: @media screen and (min-width: 960px) and (max-width:1199px){ .grid-co ...

Refreshing the status of object properties in a React application

I stored an array in state like: const Theme = { name: "theme", roots: { theme: Theme, }, state: { theme: { quiz: { quizGender: null, quizSleepComfort: { justMe: { soft: null, ...

How can you implement a switchable theme feature similar to Twitter on your website?

As a beginner programmer and college student with limited design knowledge, I am working on creating a microblogging website similar to Twitter. I noticed that some websites use pre-loaded styles or themes without separate CSS files. I am unsure of how t ...

A guide on organizing a JSON object in JavaScript/AngularJS through grouping

My JSON object contains zip code entries organized by the "Zipcode" key. My goal is to group these entries by zip code to display which zone IDs are associated with each zip code. For example, zip code 7 corresponds to zone IDs 12005, 12008, 12006, and 1 ...

"Encountering a puzzling issue with Django Rest Framework where the path setup is functioning for one link but not for

I'm currently attempting to connect to my MySQL database using the Django REST backend. On my frontend, I'm using Vue with Axios. Specifically, I have a junction table called TeacherSubjectJunction, and I want to access it through the following p ...

Error: The 'encoding' property cannot be set on null objects in THREE.js

I have been trying to load a basic grass block from Minecraft, but I keep encountering an error message that says TypeError: Cannot set properties of null (setting 'encoding'). This error originates from line 2893 in GLTFLoader, specifically the ...

Is there a method available to minimize the size of a local storage list containing strings?

Hey there, I am trying to load a large 2.5MB json file in my browser so that I can use it for some typeAhead functions. Unfortunately, I'm facing an issue with my local storage being constantly full. When using Firefox, I receive the following error ...

Challenges with parsing JSON using jQuery

I am attempting to retrieve data from a page that returns JSON in order to store it in an array. The current code is functional, but I am encountering difficulties when trying to pass the variable (which should contain the content) into the jQuery.parseJSO ...

Combining Angular 2 and Symfony 3 for seamless integration

I am currently working on integrating Angular 2 with Symfony 3. After using npm to install node modules, I encountered an issue linking them from my base view base.html.twig. Despite adding scripts, none of them seem to be working or appearing in the page& ...