0

I have two json objects as follow

Id:1

 {"points":[{"x":109,"y":286,"r":1,"color":"black"},{"x":108,"y":285,"r":1,"color":"black"},{"x":106,"y":282,"r":1,"color":"black"},{"x":103,"y":276,"r":1,"color":"black"},],"lines":[{"x1":109,"y1":286,"x2":108,"y2":285,"strokeWidth":"2","strokeColor":"black"},{"x1":108,"y1":285,"x2":106,"y2":282,"strokeWidth":"2","strokeColor":"black"},{"x1":106,"y1":282,"x2":103,"y2":276,"strokeWidth":"2","strokeColor":"black"}]}

Id-2

{"points":[{"x":524,"y":343,"r":1,"color":"black"},{"x":523,"y":342,"r":1,"color":"black"},{"x":521,"y":339,"r":1,"color":"black"},{"x":520,"y":334,"r":1,"color":"black"},{"x":514,"y":319,"r":1,"color":"black"}],"lines":[{"x1":524,"y1":343,"x2":523,"y2":342,"strokeWidth":"2","strokeColor":"black"},{"x1":523,"y1":342,"x2":521,"y2":339,"strokeWidth":"2","strokeColor":"black"},{"x1":521,"y1":339,"x2":520,"y2":334,"strokeWidth":"2","strokeColor":"black"},{"x1":520,"y1":334,"x2":514,"y2":319,"strokeWidth":"2","strokeColor":"black"}]}

I am trying to merge these two data onto a canvas I am able to retrieve a single file but combining them i am not able to do

def loadDrawing(request):
    """ Function to load the drawing with drawingID if it exists."""
    try:
        # Getting JSON object string of saved drawing.
        drawingJSONData = Drawing.objects.get(id = 1).drawingJSONText

        # drawingJSONData1 = Drawing.objects.get(id=1).drawingJSONText
       # drawingJSONData2 = Drawing.objects.get(id=2).drawingJSONText

        # Seding context with appropriate information
        context = {
            "loadIntoJavascript" : True,
            "JSONData" : drawingJSONData

        }
        # Editing response headers and returning the same
        response = modifiedResponseHeaders(render(request, 'MainCanvas/index.html', context))
        return response

My model in django

class Drawing(models.Model):
    drawingJSONText = models.TextField(null = True)

My .js file to load the drawing and parsing it from server to JSON object and pushing the loaded points into an array

 // Checking if the drawing to be loaded exists
    if (document.getElementById('JSONLoadData') != null)
    {

        // Parsing the loaded drawing from server to a JSON Object
        var loadedData = JSON.parse(JSONLoadData.value)

        // Iterating through all the points in the loaded drawing
        for(let i = 0; i < loadedData['points'].length; i++)
        {
            // Saving the point and drawing the same in the svg canvas
            const point = svg.append('circle')
                             .attr('cx', loadedData['points'][i]['x'])
                             .attr('cy', loadedData['points'][i]['y'])
                             .attr('r', loadedData['points'][i]['r'])
                             .style('fill', loadedData['points'][i]['color']);

            // Pushing the point inside points array
            points.push(point);
        }

        // Iterating through all the lines in the loaded drawing
        for(let i = 0; i < loadedData['lines'].length; i++)
        {
            // Saving the line and drawing the same in the svg canvas
            const line = svg.append('line')
                            .attr('x1', loadedData['lines'][i]['x1'])
                            .attr('y1', loadedData['lines'][i]['y1'])
                            .attr('x2', loadedData['lines'][i]['x2'])
                            .attr('y2', loadedData['lines'][i]['y2'])
                            .attr('stroke-width', loadedData['lines'][i]['strokeWidth'])
                            .style('stroke', loadedData['lines'][i]['strokeColor']);

            // Pushing the line inside lines array
            lines.push(line);
        }

    }

});

Edited : If my model is as follows

class Drawing(models.Model):
    drawingJSONText = models.TextField(null=True)
    project = models.CharField(max_length=250)

How can i filter data based on project Lets say i have three datasets

1st one contains project = a
2nd one contains project = b
3rd one contains project = a
4th one contains project = a

How can i add datapoints like above by filtering data Drawing.objects.filter(project=a) then based on the queryset i have three data points and corresponding data are plotted on canvas as above

1 Answer 1

1

I'm not entirely sure this is what you want, but are you trying to combine id-1 and id-2? If I think I understand what you are trying to do, will using the + operator work for you in this case?

drawingJSONData1 = json.loads(Drawing.objects.get(id=1).drawingJSONText)
drawingJSONData2 = json.loads(Drawing.objects.get(id=1).drawingJSONText)

drawingJSONData = dict()
drawingJSONData["points"] = drawingJSONData1["points"]+drawingJSONData1["points"]
drawingJSONData["lines"] = drawingJSONData2["lines"]+drawingJSONData2["lines"]

With your example above, you'd end up with:

{'points': [{'x': 109, 'y': 286, 'r': 1, 'color': 'black'},
  {'x': 108, 'y': 285, 'r': 1, 'color': 'black'},
  {'x': 106, 'y': 282, 'r': 1, 'color': 'black'},
  {'x': 103, 'y': 276, 'r': 1, 'color': 'black'},
  {'x': 524, 'y': 343, 'r': 1, 'color': 'black'},
  {'x': 523, 'y': 342, 'r': 1, 'color': 'black'},
  {'x': 521, 'y': 339, 'r': 1, 'color': 'black'},
  {'x': 520, 'y': 334, 'r': 1, 'color': 'black'},
  {'x': 514, 'y': 319, 'r': 1, 'color': 'black'}],
 'lines': [{'x1': 109,
   'y1': 286,
   'x2': 108,
   'y2': 285,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 108,
   'y1': 285,
   'x2': 106,
   'y2': 282,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 106,
   'y1': 282,
   'x2': 103,
   'y2': 276,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 524,
   'y1': 343,
   'x2': 523,
   'y2': 342,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 523,
   'y1': 342,
   'x2': 521,
   'y2': 339,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 521,
   'y1': 339,
   'x2': 520,
   'y2': 334,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 520,
   'y1': 334,
   'x2': 514,
   'y2': 319,
   'strokeWidth': '2',
   'strokeColor': 'black'}]}

EDIT: added the gets wrapped in json.loads to convert string to Python object as I don't know what kind of field that is and given the error being seen.

Sign up to request clarification or add additional context in comments.

9 Comments

I get a type error "string indices must be integers"
I've edited answer a bit. You may have to convert the strings you have in those fields to a python object for it to work properly. Edit your question to show what your Drawing model looks like.
I dont get an error now , but i get a blank page no coordinates are loaded onto the canvas.
guessing you need to convert back to a string? in your context try "JSONData" : json.dumps(drawingJSONData),
extension to above , if lets say i have to filter the data based on another data in model then does the '+' work.Edited model
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.