1

How would you color a polygon/line/dot based on a date? The colors would be a pre-set list. I need the area to update its color based on the current (or given) date. I would be using tilemill or cartodb.

Example:

Polygon A:

  • Color would be white after Jan 1,
  • Color would be grey after Feb 15
  • Color would be brown after March 1, etc.

    Line B:

  • Color would be blue after March 15,
  • Color would be white after Dec 1, etc.

    1

    You could likely do this if you supplied the dates in for your segments in CartoDB, selected only the data you need, and you can then color it using CartoCSS. For example, you could have data such as this in CartoDB:

                the_geom      name  type     col_date_start col_date_end       color       
     Row 1:    (geometry)      a    polygon      0101            0215          white
     Row 2:    (geometry)      a    polygon      0215            0301          gray
     Row 3:    (geometry)      a    polygon      0301            ....          brown
     ....
     Row 8:    (geometry)      b    polyline     0101            0315          blue
     Row 9:    (geometry)      b    polyline     1201            0232          white
    

    So now that your data is set up, you could probably then set up a query like this:

    SELECT * FROM my_table, to_char(current_timestamp, 'MMDD') AS date_compare 
    WHERE date_compare::int > col_date_start AND date_compare:int < col_date_end;
    

    This would only return the polygon data that is relevant for the current time of the page load. You could then use CartoCSS to format your data like so:

    #my_table {
      [type = 'polygon'] {
        [color='white']
          {polygon-fill:#FFFFFF;}
        [color='gray']
          {polygon-fill:#666666;}
        [color='brown']
          {polygon-fill:#B26B24;}
      }
      [type = 'polyline'] {
        [color='white']
          {line-color:#FFFFFF;}
        [color='blue']
          {line-color:#0000FF;}
      }
    }
    

    Not the most elegant of a solution, but I think it should work for your case.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.