2
\$\begingroup\$

I have elements that are generated dynamically on the screen. I have bars and diamonds.

enter image description here

The problem is I have hundreds of different types of bars or diamonds that need to be rendered on the screen. The bars and diamonds are tasks and milestones on a gantt chart. There are hundreds of different types and each type has to have a certain image attached to it for the background so they can be identified a little more easily.

Instead of writing a huge css file with background-image style I thought it would be better to dynamically generate a css class with the background-image: url("~/relativepath/imageName") style.

Each one of these different types of elements have their own image file associated with them. Every element also has a Code property such as EX0blue. I was thinking of changing the image file names to match the code property, so when the css is generated I can give it a class name of the code and a relative path such as background-image: url("~/relativepath/code.jpg")

Is there a more optimal solution than what I am trying to implement?

Here is what I have so far

class CssGenerator {

    constructor() {
        this.relativeImagePath = '~/Content/images/activityTypes/';
    }


    createImageClass(code) {

        let style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = '.' + code +' { background-image: url(' + relativeImagePath + code + '.jpg); }';
        document.getElementsByTagName('head')[0].appendChild(style);
    }

}

Then after that I would add the class like this

gantt.templates.task_class=function(start, end, task){
 this.cssGenerator.createImageClass(task.code)
return task.code;
};

That return statement applies the string returned as a classname to the dynamic element. It's from the API

\$\endgroup\$
10
  • 2
    \$\begingroup\$ Performance-wise, using canvas/svg might be a better idea. \$\endgroup\$
    – woxxom
    Commented Feb 24, 2017 at 16:10
  • \$\begingroup\$ @wOxxOm Thanks, performance is actually really important here. \$\endgroup\$
    – Train
    Commented Feb 24, 2017 at 17:23
  • 2
    \$\begingroup\$ It might be simpler just to set the style property on each element rather than creating a separate stylesheet for each one. \$\endgroup\$ Commented Feb 24, 2017 at 18:34
  • \$\begingroup\$ One more thought, this might be a good option for a library like D3. I also don't think that you can use a tilde in a CSS Url. \$\endgroup\$ Commented Feb 24, 2017 at 18:37
  • \$\begingroup\$ You might want to discuss what these bars and diamonds are, what they do, and why there's a hundred of them rather than your approach. This post reeks of the XY Problem. There might be better ways to do this, but first we need to know what you are doing and why. \$\endgroup\$
    – Joseph
    Commented Feb 24, 2017 at 19:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.